Sorting an array of more than 10 objects in Chrome
arrays, google-chrome, javascript, safari, sorting
Solution
It doesn't matter the number of items, it may be just your case. It's just that's not the correct way to implement a sorting function. You should return 1 (or any positive value, if the first item is "greater" than the second), -1 (or any negative value) or 0 (if the items are equal). Try something like this:
.sort(function(a, b) {
return a.tag > b.tag ? 1 : (a.tag < b.tag ? -1 : 0);
});
The parentheses around the last ternary operator aren't actually necessary.
Problem
``` [ {"tag":"fujairah","count":1}, {"tag":"worldwide","count":3}, {"tag":"saudi","count":1}, {"tag":"miami","count":1}, {"tag":"rwo-dealer","count":7}, {"tag":"new york","count":2}, {"tag":"surabaya","count":1}, {"tag":"phillippines","count":1}, {"tag":"vietnam","count":1}, {"tag":"norway","count":1}, {"tag":"x","count":1} ].sort(function(a,b){return a.tag>b.tag}) ``` Sorting an array of 10 objects works fine, once the number of objects exceeds 10, the sort fails in Chrome for Mac. Safari fails at any array size. (Works fine in Firefox) What is the correct way of sorting arrays of objects by javascript?