I have an array of objects:
var countries = [
{'id': 35, 'name': 'Перу'},
{'id': 45, 'name': 'Індія'},
{'id': 55, 'name': 'Єгипет'},
{'id': 65, 'name': 'Албанія'},
]
I need to sort it by name. Here is my algorithm:
function mySort(s1, s2) {
return s1.name.toString().localeCompare(s2.name.toString());
}
var sorted = countries.sort(mySort);
// result (incorrect)
sorted = [
{'id': 55, 'name': 'Єгипет'},
{'id': 45, 'name': 'Індія'},
{'id': 65, 'name': 'Албанія'},
{'id': 35, 'name': 'Перу'},
]
// must be
sorted = [
{'id': 65, 'name': 'Албанія'},
{'id': 55, 'name': 'Єгипет'},
{'id': 45, 'name': 'Індія'},
{'id': 35, 'name': 'Перу'},
]
What am I doing wrong?
Here is example on jsfiddle and screenshot of my result in jsfiddle.

Thanks!
EDIT:
I’ve found that only Google chrome sorts incorrect: FireFox and Opera do it correct.
Your code is working as you expected for me.
It doesn’t make a functional difference, but you don’t need to call
.toString()on something that is already a String. I.E., you can simplifymySortto this:(I also prefer to declare all functions as variables, as they’re all the same in JavaScript – so they should be handled consistently.)
However, as to answer why this may not be working for you, per http://msdn.microsoft.com/en-us/library/windows/apps/62b7ahzy%28v=vs.94%29.aspx (interestingly, MDN’s page doesn’t say anything about this) – emphasis mine:
If you want something that will sort purely on the character codes without regards to the user’s locale, you could use this instead:
This will sort consistently, regardless of the client’s or user’s settings / preferences – but it also returns the results you classified as incorrect.
Edit: As you mentioned that the behavior you’re observing is specific to Google Chrome, this appears to be a known issue in Chrome, and is being tracked at http://code.google.com/p/v8/issues/detail?id=459.