Sort by two values prioritizing on one of them
Solution 1:
(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;
}));
Solution 2:
A simple solution is:
data.sort(function (a, b) {
return a.count - b.count || a.year - b.year;
});
This works because if count is different, then the sort is based on that. If count is the same, the first expression returns 0 which converts to false and the result of the second expression is used (i.e. the sort is based on year).