In JS, sort is used in combination with localeCompare to implement a Chinese sorting instance. sortlocalecompare

Source: Internet
Author: User

In JS, sort is used in combination with localeCompare to implement a Chinese sorting instance. sortlocalecompare

When it comes to table sorting, the first thing we should talk about is array sorting, 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:
Copy codeThe 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:
Copy codeThe 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

Copy codeThe 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 "){
Return-1;
}
// 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:
Copy codeThe 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
}
));


When comparing strings using localeCompare () in js, only the first character is compared?

StringVar. localeCompare (stringExp) Where stringVar is required. The post-text of a String object.
StringExp is required. String to be compared with stringVar.

LocaleCompare allows you to compare stringVar and stringExp strings with region-specific settings and return-1, 0, or + 1, depending on the sorting of default region settings in the system. If the stringVar sorting is before stringExp, localeCompare returns-1; if the stringVar sorting is after stringExp, + 1 is returned. If the return value is 0, the two strings are the same.
 
When localeCompare is used to sort Chinese Characters in Chinese pinyin, the knot is not beautiful. How can this problem be solved?

Var citys = ['Beijing-B ', 'shanghai-S', 'guangzhou-G', 'shenzhen-S', 'nanjing-n', 'gusu-S ', 'hangzhou-H', 'jinan-J', 'qingdao-Q', 'wuhan-W', 'shenyang-Sh', 'chengdu-Ch ', 'tianjin-t', 'chongqing-Ch', 'xi'an-x', 'zhengzhou-zh ', 'shijiazhuang-Sh', 'changsha-Ch ', 'changchun-Ch', 'hefei-H', 'fuzhou-F']; function sortRule (a, B) {return. replace (/. *\-(. +) $/, '$ 1')> B. replace (/. *\-(. +) $/, '$ 1');} window. onload = function () {alert (citys. sort (sortRule);} cannot be correctly sorted on the safari browser... Firefox can

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.