samedi 25 avril 2015

ActionScript 3 sort an array with numbers and letters not working as expected


I'm trying to sort the users cards in a card game, for example all user's aces to stand close to each other. I'm using this:

p1cards is an array with elements like "c8", "d9", "h1", letters stand from card symbol (Club, Diamond, Hearts) and number is the card value (1 is Ace, 2 is 2, and so on)

p1cards.sort(sortOrder);

    function sortOrder(a,b)
    {
        var aN = parseInt(a.substr(1));
        var bN = parseInt(b.substr(1));

        if (aN > bN)
        {
            return 1;
        }
        else
        {
            return -1;
        }
    }

The problem is that sorted card like the 8d, 8c are switching places to 8c, 8d, kind of randomly, when I draw a new card.

Any help will be apreciated.

See in picture below:

http://ift.tt/1I9gxbu


Aucun commentaire:

Enregistrer un commentaire