I have an array of objects, for example:
var a = [
{ value: 500, name: 'ccc' },
{ value: 100, name: 'bbb' },
{ value: 500, name: 'aaa' },
{ value: 300, name: 'eee' },
];
And I need to sort it by descending order of value field, AND if value field are equals -- then sort this two objects by alphabet order of field name.
I try something like this:
a.sort(function (a, b) {
return b["value"] - a["value"] || (a["name"] > b["name"]) ? 1: -1;
});
But this does not result in
500,aaa
500,ccc
300,eee
100,bbb
as I would expect