Home > Web Front-end > JS Tutorial > JavaScript example code for sorting Chinese characters_javascript skills

JavaScript example code for sorting Chinese characters_javascript skills

WBOY
Release: 2016-05-16 19:03:29
Original
987 people have browsed it

For example, var arr = ["中","华","人","民","Communist","和","国"], after executing the sort method, the result is: China, People, Communist Party, China, He, Guo, Min, are neither pinyin nor the order of the number of strokes.
I have rarely paid attention to the localeCompare method before. The manual says that it returns a value when executed, indicating whether the two strings are the same in the current locale. There are three types of return values: -1, 0, 1, which are exactly the types required by the sort method parameters. Apply localeCompare to the sort method and see the result:

var arr = ["中","华","人","民","公","和","国" ];
alert(arr.sort(function(a,b){return a.localeCompare(b)}));//The result is: Communist, Guo, He, Hua, Min, Ren, Zhong (pinyin ascending order )

Note that the localeCompare method compares in the current area, that is, the comparison environment is the geographical area set by the current operating system, such as the representation of numbers, currency, date and time, etc. (in the "Control Panel" You can check and modify in ), if the sorting method is set to stroke, the above result will be: People, China, People's Republic of China, Communist Party, China, and Country (in ascending order of strokes).

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template