Saturday, May 11, 2024
 Popular · Latest · Hot · Upcoming
193
rated 0 times [  195] [ 2]  / answers: 1 / hits: 30545  / 13 Years ago, tue, august 2, 2011, 12:00:00

There are a lot of examples for sorting some JSON array by some property (i.e. 'title')
We are using compare function like this one:



function sortComparer(a, b) {
if (a.title == b.title)
return 0;
return a1 > b1 ? 1 : -1;
}


Problem is that Serbian Latin alphabet order looks like A, B, C, Č, Ć, D,...
When using sortComparer above I am getting D sorted before Č or Ć.
Any idea how to sort respecting current culture language?


More From » sorting

 Answers
31

If the locale in your system is set correctly then you can use localeCompare method instead of greater-than operator to compare the strings - this method is locale aware.



function sortComparer(a,b){
return a.title.localeCompare(b.title)
};

[#90871] Sunday, July 31, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
karyme

Total Points: 545
Total Questions: 102
Total Answers: 120

Location: French Polynesia
Member since Tue, Jul 7, 2020
4 Years ago
;