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

JavaScript example code for sorting Chinese characters_javascript skills

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-05-16 19:03:29
Original
1029 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:
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
Latest Issues
What are JavaScript hook functions?
From 1970-01-01 08:00:00
0
0
0
What is JavaScript garbage collection?
From 1970-01-01 08:00:00
0
0
0
c++ calls javascript
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template