I have an array of JavaScript objects:
This is not a duplicate question.
Because, I have an array of objects that has 2 keys (key, count).
I wanted to sort, key in ascending( which is string) AND value in descending(which is number) order.
var array = [
{"count":7,"key":"a"},
{"count":10,"key":"b"},
{"count":5,"key":"c"},
{"count":10,"key":"a"},
{"count":3,"key":"d"}
];
Desired Output:
var array = [
{"count":10,"key":"a"},
{"count":10,"key":"b"},
{"count":7,"key":"a"},
{"count":5,"key":"c"},
{"count":3,"key":"d"}
];
var array = [{"count":7,"key":"a"},{"count":10,"key":"b"},{"count":5,"key":"c"},{"count":10,"key":"a"},{"count":3,"key":"d"}];
console.log(array.sort((a, b) => (b.count - a.count)));
key sort as ascending
count sort as descending
I have used array.sort((a, b) => (b.count - a.count)) method for sorting count. but, Can't figure out how to sort both the keys of object.