I am trying to sort the list by "Status" such that objects with "status" : "NOT_FOUND" appears first then the "status" : "OK" . That is in Alphabetical order
let elements = [
{
"distance" : {
"text" : "1.6 km",
"value" : 1550
},
"duration" : {
"text" : "6 mins",
"value" : 358
},
"status" : "OK"
},
{
"status" : "NOT_FOUND"
}
];
//sort the elements
elements.sort(function(a, b) {
if (typeof a.status !== 'undefined' && typeof b.status !== 'undefined') {
return b.status - a.status
}
return 0
});
console.log(elements);
CodePudding user response:
-
is for numbers. Use localeCompare instead.
b.status.localeCompare(a.status);