Sample Code for sorting Chinese characters using JavaScript

Source: Internet
Author: User

For example, VAR arr = ["中"", "", "and", ""], after executing the sort method, the result is: the sorting of Chinese characters, Chinese characters, and Chinese characters is neither pinyin nor the number of strokes.
I have seldom noticed the localecompare method before. In the manual, a value is returned when it is executed, indicating whether two strings are the same in the current region settings. There are three return values:-, 1, which is exactly the type required by the sort method parameter. Apply localecompare to the sort method and check the result:

VaR arr = ["中"", "", "and", ""];
Alert (ARR. sort (function (a, B) {return. localecompare (B)}); // The result is: total, country, and, Hua, Min, man, medium (pinyin ascending)

Note that the localecompare method is compared in the current region, that is, the compare environment is the geographic region set by the current operating system, for example, the expression of numbers, currencies, date and time (you can view and modify them in the control panel). If you set the sorting method to stroke, the above result will be: person, china, the people, the Communist Party of China, and the State (in ascending order of strokes ).

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.