I am sorting my array in JavaScript based on names after then I am sorting it based in joinDate but somehow it is not checking for joiningDate.
Unfortunately, I can't use an if-else condition - I only want to use ternary operators.
My code is like this:
person.sort(((a, b) => (a.name > b.name) ? 1 : (a.joinDate > b.joinDate) ? 1 : -1));
It is sorting the names but not sorting the joinDate property
My list look like this:
{
"data": [
{
"id": "fdsf",
"name": "Julie",
"joinDate": "01/10/2019"
},
]
}
-1ifa.name < b.name, rather than fall back to comparing thejoinDateproperties. This will rapidly get very messy to do with nested ternaries - as a general rule I would say NEVER nest more than 2 ternaries, and be very wary of even nesting 2 of them.joinDate, i.e.2019-10-01(yyyy-MM-dd) instead of01/10/2019. One of the main advantages is that they sort naturally alphanumerically.