You cannot use the operator -for strings; they are only for numbers. For leksigraficheskogo order you need to use <, >, <=or >=.
Nina Scholz's answer is very useful when you work on non-English words, and he works on English words.
A simple change -to >will cause the code to work:
var city = [{
"city": "London"
}, {
"city": "Wales"
}, {
"city": "Atom"
}, {
"city": "Bacelona"
}];
city.sort(function(a,b){
return (a.city > b.city ? 1 : (a.city === b.city ? 0 : -1));
});
console.log(city);
source
share