0

I have a big array containing 52 objects, each representing a specific card in a deck of cards.

var deck = [{'suit': 's', 'value' : A}, {'suit': 'h', 'value' : 9}...]

srepresenting the spade suit, and A for the Ace of spades in this case, and so on.

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 <ul>on my page.

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:

♠89A, ♥A29J, ♦KTJ37, ♣8

Which is not optimal.

I would like to be able to sort the cards from A to 2, e.g. ♠AJ72, ♥JT92.. and so on.

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: Liveweave (pretty sweet codeapp i must say)

This is my javascript:

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 < 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;
        }
    }
}
Dhanu Gurung
  • 8,480
  • 10
  • 47
  • 60
XT_Nova
  • 1,110
  • 5
  • 17
  • 32
  • +1 for your effort. The link to your code does not seem to work for me. Kindly check it out. – Talha Masood Dec 18 '13 at 05:54
  • Here are a few links that discuss how to sort in JavaScript: [one](http://stackoverflow.com/q/979256/778118), [two](http://www.sitepoint.com/sophisticated-sorting-in-javascript/), [three](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort)... – jahroy Dec 18 '13 at 05:55
  • @TalhaMasood hmm, the link works for me. – XT_Nova Dec 18 '13 at 05:56
  • 1
    Take a look at the Array.sort function on Mozilla's Developer Network: http://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort – Álvaro Martínez Dec 18 '13 at 05:56
  • [Here](http://liveweave.com/ztpGeW) is another way to create your deck. – jahroy Dec 18 '13 at 06:19

1 Answers1

0

You have to write a custom comparator function and pass it to the sort function of your hand array.

hand.sort(function(a,b) {
    var values = {A:13,K:12,Q:11,J:10};
    var c = a.value;
    var d = b.value;

    if(c.match(/[A-Z]/) != null) {
        c = values[c];
    }
    if(d.match(/[A-Z]/) != null) {
        d = values[d];
    }

    return d-c;
});
Samuel
  • 2,106
  • 1
  • 17
  • 27
  • 3
    this is more of a comment than an answer. – abc123 Dec 18 '13 at 06:00
  • @jahroy I agree that htmlNewbie made a mistake asking this question without showing a specific issue. Since this isn't a difficult task for research. However, Samy shouldn't be posting answers that are comments...this was my attempt at getting Samy to realize that it is better to post these type of things as Comments, I already voted duplicate on the question. – abc123 Dec 18 '13 at 06:07