How do I use JavaScript to sort Chinese?

Source: Internet
Author: User

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?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.