How would I sort this data by count
and year
values in ascending order prioritizing on the count
value?
//sort this var data = [ { count: '12', year: '1956' }, { count: '1', year: '1971' }, { count: '33', year: '1989' }, { count: '33', year: '1988' } ]; //to get this var data = [ { count: '1', year: '1971' }, { count: '12', year: '1956' }, { count: '33', year: '1988' }, { count: '33', year: '1989' }, ];
JavaScript Array sort() The sort() sorts the elements of an array. The sort() overwrites the original array. The sort() sorts the elements as strings in alphabetical and ascending order.
(See the jsfiddle)
var data = [ { count: '12', year: '1956' }, { count: '1', year: '1971' }, { count: '33', year: '1989' }, { count: '33', year: '1988' } ]; console.log(data.sort(function (x, y) { var n = x.count - y.count; if (n !== 0) { return n; } return x.year - y.year; }));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With