Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do i sort my javascript objects when taking them from an array?
    primarykey
    data
    text
    <p>I have a big array containing 52 objects, each representing a specific card in a deck of cards. </p> <p><code>var deck = [{'suit': 's', 'value' : A}, {'suit': 'h', 'value' : 9}...]</code></p> <p><code>s</code>representing the spade suit, and <code>A</code> for the Ace of spades in this case, and so on.</p> <p>I have managed (thanks to some guidance from friendly souls here on stackoverflow) to randomize this deck, added 13 of these to a player, and got those to show in an <code>&lt;ul&gt;</code>on my page.</p> <p>My problem is, that the values form the deck array i add to the player, i am adding as it is, meaning, the output could be: </p> <p>&spades;89A, &hearts;A29J, &diams;KTJ37, &clubs;8</p> <p>Which is not optimal.</p> <p>I would like to be able to sort the cards from A to 2, e.g. &spades;AJ72, &hearts;JT92.. and so on.</p> <p>Since the deck array will take a huge amount of space, i'm deleteing it from the code i show here. But here is the whole code: <a href="http://liveweave.com/oF30kB" rel="nofollow">Liveweave (pretty sweet codeapp i must say)</a></p> <p>This is my javascript:</p> <pre><code>var deal = function () { //Player hands var north_hand = []; var east_hand = []; var south_hand = []; var west_hand = []; //Creating the deck var deck = [{'suit': 's', 'value': 'A'}, ... //+51 more cards]; //Call the shuffleDeck function shuffleDeck(deck); north_hand = deck.slice(0, 13); east_hand = deck.slice(13, 26); south_hand = deck.slice(26, 39); west_hand = deck.slice(39, 52); var north_spades = document.getElementById('p1_spades'); var north_hearts = document.getElementById('p1_hearts'); var north_diamonds = document.getElementById('p1_diamonds'); var north_clubs = document.getElementById('p1_clubs'); for (var i = 0; i &lt; north_hand.length; i++) { if (north_hand[i].suit == "s") { north_spades.innerHTML += north_hand[i].value; } else if (north_hand[i].suit == "h") { north_hearts.innerHTML += north_hand[i].value; } else if (north_hand[i].suit == "d") { north_diamonds.innerHTML += north_hand[i].value; } else { north_clubs.innerHTML += north_hand[i].value; } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload