It's often necessary to sort the list when it's displayed on a Web page: Sort by the modified/accessed time, by region, by name. This article and everyone to share is the use of JavaScript to the Chinese sort related content, come to see it, I hope to everyoneLearn JavaScripthelpful.
The Chinese list is sorted alphabetically by name, not simply by string comparison-' A ' > ' B '-this way.
For example compare ' Beijing ' vs ' Shanghai ', actually compare ' Běijīng ' vs ' shànghǎi '; compare ' Beijing ' vs ' background ', actually compare ' Běijīng ' vs ' bèijǐng '.
generally need to get to the string of pinyin, and then compare the respective pinyin.
JavaScript provides localized text ordering, such as sorting Chinese alphabetically, without requiring the program to display the comparison string pinyin.
String.prototype. Localecompare in the premise of not considering Polyphone, the basic can be perfectly implemented according to phonetic sorting.
in the absence of an accident, each browser that supports Localecompare is normal. Recently updated Chrome to 58.0.3029.110, suddenly found that the Chinese sort is not normal.
//Normal should return 1, pinyin Jia in front, Kai in the rear ' open '. Localecompare (' drive ');//Get
-1;
//Chrome 58.0.3029.110 back-1, other browsers normal
//After confirmation is localecompare need to explicitly specify locales parameter ' on '. Localecompare (' drive ', ' zh ');//Get 1
Pass the locales parameter under Chrome to get normal expected results
Edge Browser Support Localecompare
Firefox Browser Support Localecompare
IE 11 Browser support Localecompare
Source: Writing
How do I use JavaScript to sort Chinese?