Using sort and localeCompare in JS to implement Chinese sorting _ javascript skills

Source: Internet
Author: User
This article mainly introduces how to use sort and localeCompare in JS to implement Chinese sorting. It focuses on the localeCompare function. If you need it, refer to Table sorting, the first thing we need to talk about is the sorting of arrays, Because array sorting is the basis of table sorting.

JavaScript provides the sort () method for Arrays for table sorting. By default, this method will sort the arrays in Array in the order of ASCII codes, javaScript also provides array reverse-order reverse () for arrays ().

Take a look at the example:

The Code is as follows:

Function sortArray (){
Var arrayTest = ["z", 5, 2, "a", 32,
ArrayTest. sort ();
Alert (arrayTest. toString (); // output: 2, 3, 32, 5, a, z
ArrayTest. reverse ();
Alert (arrayTest. toString (); // output: z, a, 5, 32, 3, 2
SortArray ();

Haha, 5 is bigger than 32. Obviously this is not the result we want. We have already said that the sort () method is sorted by ASCII code.

In fact, the sort () method also allows a parameter of the function type, which can be called a comparison function. When the comparison function can receive two parameters, the meaning of the return value of the function is as follows:

The Code is as follows:

-1: The first parameter is smaller than the second parameter.
0: the first parameter is equal to the second parameter.
1: The first parameter is greater than the second parameter

The Code is as follows:

* Comparison functions
* @ Param {Object} param1 parameter 1 to be compared
* @ Param {Object} parameter 2 to be compared
* @ Return {Number} If param1> param2, 1 is returned.
* If param1 = param2, 0 is returned.
* If param1 <param2 returns-1
Function compareFunc (param1, param2 ){
// If both parameters are strings
If (typeof param1 = "string" & typeof param2 = "string "){
Return param1.localeCompare (param2 );
// If parameter 1 is a number and parameter 2 is a string
If (typeof param1 = "number" & typeof param2 = "string "){
// If parameter 1 is a string and parameter 2 is a number
If (typeof param1 = "string" & typeof param2 = "number "){
Return 1;
// If both parameters are numbers
If (typeof param1 = "number" & typeof param2 = "number "){
If (param1> param2) return 1;
If (param1 = param2) return 0;
If (param1 <param2) return-1;

When we execute arrayTest. sort (compareFunc), we get the correct result.
Here, we have to explain the usage of the localeCompare () method. This method sorts strings and has only one parameter, that is, the string to be compared.

The details are as follows:

1. If the String object is placed before the String in the parameter in alphabetical order, a negative number is returned.
2. If the String object is placed after the String in the parameter in the character order, a positive number is returned.
3. If the String object is equal to the String in the parameter, 0 is returned.

In addition, the localeCompare () method has its own uniqueness, which can be reflected in its method signature locale (on-site, local, that is to say, his implementation follows the regional characteristics. In the English system, his implementation may follow the string ascending order. If he is in Chinese, its implementation is based on the first letter of pinyin.

That is to say, even if we involve Chinese characters in the program, we will not return errors in sorting.
Refer to the following procedure:

The Code is as follows:

Var testArray = ["feet", "Ben", "Zhi", "home"];
Document. write (testArray. sort (
Function compareFunction (param1, param2 ){
Return param1.localeCompare (param2); // output: Home, local, and foot

Related Article

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: 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.