I'm very new to JavaScript and am trying to learn it right now. I have a few exercises that I just want to solve and where I get stuck. It's about sorting. Maybe it's a fundamental problem of understanding on my part. I have an array that I want to sort. The array has a sorting sequence process (Carddeck):
let RANKS = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A'];
let SUITS = ['A', 'B', 'C', 'D'];
the array to sort:
let arrayToSort = ['10B', '9D', '9C', '9A', 'AA']
// => ['9A', '9C', '9D', '10B', 'AA']
I thought that I would need a compareFunction(a, b) for indexOf RANKS and SUITS.
.sort(function (a, b)
{
if ( a.suitIndex < b.suitIndex
|| ( a.suitIndex == b.suitIndex && a.rankIndex < b.rankIndex )
) {
return -1;
}
return 1;
});
Then I tried to find out the index value.
let RANKS = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A'];
let SUITS = ['A', 'B', 'C', 'D'];
let arrayToSort = ['10B', '9D', '9C', '9A', 'AA']
let card1 = arrayToSort.slice(0, 1).join()
let card2 = arrayToSort.slice(1, 2).join()
let card3 = arrayToSort.slice(2, 3).join()
let card4 = arrayToSort.slice(3, 4).join()
let card5 = arrayToSort.slice(4, 5).join()
console.log( card1, RANKS.indexOf(card1.slice(0, -1)), SUITS.indexOf(card1.charAt(card1.length - 1)))
console.log( card2, RANKS.indexOf(card2.slice(0, -1)), SUITS.indexOf(card2.charAt(card2.length - 1)))
console.log( card3, RANKS.indexOf(card3.slice(0, -1)), SUITS.indexOf(card3.charAt(card3.length - 1)))
console.log( card4, RANKS.indexOf(card4.slice(0, -1)), SUITS.indexOf(card4.charAt(card4.length - 1)))
console.log( card5, RANKS.indexOf(card5.slice(0, -1)), SUITS.indexOf(card5.charAt(card5.length - 1)))
.as-console-wrapper { max-height: 100% !important; top: 0; }
At this point I get stuck. I don't want a solution at first. I want to understand where my fault is and which way I should rather look. I would be happy to receive a tip. Thank you.