# javaScript: sorting an array with multiple object/properties

**URL:** <https://forum.freecodecamp.org/t/javascript-sorting-an-array-with-multiple-object-properties/330484>\
**Category:** General\
**Created:** [December 4, 2019, 2:10am UTC](https://forum.freecodecamp.org/t/javascript-sorting-an-array-with-multiple-object-properties/330484 "2019-12-04T02:10:11Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![PaolaDMadd](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/paoladmadd/32/145984_2.png) [@PaolaDMadd](https://forum.freecodecamp.org/u/PaolaDMadd)\
**Post date:** [December 4, 2019, 2:10am UTC](https://forum.freecodecamp.org/t/javascript-sorting-an-array-with-multiple-object-properties/330484/1 "2019-12-04T02:10:11Z")

</div>

Hello every one, I am trying to build a card game using javaScript.  
I have used the .sort() method to reorder my deck of cards.

My array is composed by a value: int number and a string: the card’s suit.  
I found a function to be included in the sort method (compare function) which should order the numerical part as well as the suit.

The ideal would be an array as follows:

````auto
{value : 1, suit: 'hearts'} 
{value : 2, suit: 'hearts'} 
{value : 3, suit: 'hearts'}
{value : 4, suit: 'hearts'} 
{value : 5, suit: 'hearts'}  
{value : 1, suit: 'clubs'} 
{ value : 2, suit: 'clubs'} 
{value : 3, suit: 'clubs'} 
{value : 4, suit: 'clubs'}
{value : 5, suit: 'clubs'}
etc. ]```

```//sorting code
myCards.sort((a,b) => a.value - b.value || a.suit - b.suit); ```

I found this method online at (https://stackoverflow.com/questions/13211709/javascript-sort-array-by-multiple-number-fields). This should pass a second property using the `||` operator (I also tried &&).

Unfortunately the result I get is : 

```{value : 1, suit: 'clubs'} 
{value : 1, suit: 'hearts'} 
{ value : 2, suit: 'clubs'} 
{value : 2, suit: 'hearts'} 
{value : 3, suit: 'clubs'} 
{value : 3, suit: 'hearts'}
{value : 4, suit: 'clubs'}
{value : 4, suit: 'hearts'} 
{value : 5, suit: 'clubs'
{value : 5, suit: 'hearts'} ```

how can I improve my code?
````

---

<div class="post-metadata">

**Author:** ![snigo](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/snigo/32/128765_2.png) [@snigo](https://forum.freecodecamp.org/u/snigo)\
**Post date:** [December 4, 2019, 6:44am UTC](https://forum.freecodecamp.org/t/javascript-sorting-an-array-with-multiple-object-properties/330484/3 "2019-12-04T06:44:11Z")

</div>

> [@PaolaDMadd](#):
>
> myCards.sort((a,b) =\> a.value - b.value || a.suit - b.suit);

If you want it to sort `suit` first and then `value` you need to flip your `OR` statement, so you first check suit and then value. Note, that `a.suit - b.suit` is not how you compare strings. Use `a.suit.localeCompare(b.suit)` instead.

---

<div class="post-metadata">

**Author:** ![PaolaDMadd](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/paoladmadd/32/145984_2.png) [@PaolaDMadd](https://forum.freecodecamp.org/u/PaolaDMadd)\
**Post date:** [December 4, 2019, 3:16pm UTC](https://forum.freecodecamp.org/t/javascript-sorting-an-array-with-multiple-object-properties/330484/4 "2019-12-04T15:16:02Z")

</div>

@snigo this is exactly what I was trying to do, reorder a shuffled array, taking in consideration 2 elements one of them (suit) a string; using localeCompare I was able to use the sort() method.

thanks a lot  
Paola

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [January 16, 2021, 11:31am UTC](https://forum.freecodecamp.org/t/javascript-sorting-an-array-with-multiple-object-properties/330484/5 "2021-01-16T11:31:20Z")

</div>


