ActionScript 3 sort an array with numbers and letters not working as expected -
i'm trying sort users cards in card game, example user's aces stand close each other. i'm using this:
p1cards array elements "c8", "d9", "h1", letters stand card symbol (club, diamond, hearts) , number card value (1 ace, 2 2, , on)
p1cards.sort(sortorder); function sortorder(a,b) { var = parseint(a.substr(1)); var bn = parseint(b.substr(1)); if (an > bn) { return 1; } else { return -1; } }
the problem sorted card 8d, 8c switching places 8c, 8d, kind of randomly, when draw new card.
any apreciated.
see in picture below:
you don't define in sort when values same, depending item gets slotted in a
, b
(which have no control over) determine order. tell sort function when items same:
function sortorder(a,b) { var = parseint(a.substr(1)); var bn = parseint(b.substr(1)); if(an == bn){ //they same, add secondary sort based off suit var = a.substr(0,1); //grab first character suit var bs = b.substr(0,1); if(as > bs) return 1; if(bs > as) return -1; return 0; } if (an > bn) { return 1; } else { return -1; } }
Comments
Post a Comment