


JavaScript study notes (3) Array sorting and the use of localeCompare() method in Chinese character sorting
When it comes to table sorting, the first thing to talk about must be 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 arrange the arrays in Array in the order of ASCII codes. Javascript also provides the reverse() method for arrays.
Take a look at the example:
1 function sortArray(){
2 var arrayTest = ["z",5,2,"a",32,3];
3 arrayTest.sort();
4 alert(arrayTest.to String ()); //output:2,3,32,5,a,z
5 arrayTest.reverse();
6 alert(arrayTest.toString()); //output:z,a,5,32, 3,2
7 }
8 sortArray(); Haha, 5 is larger than 32. Obviously this is not the result we want. As mentioned just now, the sort() method sorts in the order of ASCII codes. In fact, the sort() method also allows a parameter of function type, which we can call a comparison function. When the comparison function can receive two parameters, the following is the meaning of the return value of the function:
-1: the first parameter Less than the second parameter
0: The first parameter is equal to the second parameter
1: The first parameter is greater than the second parameter
Look at an example:
1 /**数 2 * Comparison function * 3 * @param {Object} param1 Parameter 1
4 * @param {Object} Param2 Parameters 2
5 *Rturn {Number} If Param1 & GT; Param2 returns 1
6 *If Param1 == Param2 returns 0
7 *If Param1 & LT; Param2 returns -1
8*/
9 function compareFunc(param1 , Param2) {m10 // If both parameters are string types
11 IF (Typeof Param1 == "String" && Typeof Param2 == "String") {
12 RETURN PARAM1.LOCALECOMPARE (Param2);
3 }
14 // If parameter 1 is a number, parameter 2 is a string
15 if (typeof param1 == "number" && typeof param2 == " ") {
16
18 // If parameter 1 is a string and parameter 2 is a number
19 if(typeof param1 == "string" && typeof param2 == "number"){
20 return 1;
21 }
22 for Digital f 23 IF (Typeof Param1 == "Number" && Typeof Param2 == "Number") {
24 if (Param1 & GT; Param2) Return 1;
25 IF (Param1 == Param2) Return 0;
26 If (if (if (if param1 < param2) return -1;
27}
28} When we execute arrayTest.sort (comparefunc), we get the right result.
At this point, we have to explain the usage of the localeCompare() method. This method is a method of sorting strings. It has only one parameter, which is the string to be compared. The specific instructions are as follows:
1. If the String object is arranged in alphabetical order before the string in the parameter, a negative number is returned
2. If the String object is arranged in alphabetical order after the string in the parameter, a positive number is returned
3. If String The object is equal to the string in the parameter and returns 0
In addition, the localeCompare() method has another unique feature, which can be reflected in its method signature locale (local, local), that is to say, his The implementation is based on regional characteristics. If it is in the English system, its implementation may be in ascending order of strings. If it is in Chinese, its implementation may be in accordance with the pinyin of the first letter. Haha, this means that even if we involve Chinese characters in the program, our sorting will not go wrong.
Refer to the following procedure:
1 var testArray = ["Zheng", "State", "Xin", "Source", "Xin", "Information", "Technology", "Technology", "Share", "Share" , "Yes", "Limited", "Gong", "Division"];
2 Document.write (TestArray.sort output: portal, public, stock, technical, technique, division, division, restriction, faith, faith, yes, source, Zheng, state
5}
6)); ) method, please pay attention to the PHP Chinese website (www.php.cn) for more related content!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Fast array sorting method in PHP that preserves key names: Use the ksort() function to sort the keys. Use the uasort() function to sort using a user-defined comparison function. Practical case: To sort an array of user IDs and scores by score while retaining the user ID, you can use the uasort() function and a custom comparison function.

To deeply understand JS array sorting: the principles and mechanisms of the sort() method, specific code examples are required. Introduction: Array sorting is one of the very common operations in our daily front-end development work. The array sorting method sort() in JavaScript is one of the most commonly used array sorting methods. However, do you really understand the principles and mechanisms of the sort() method? This article will give you an in-depth understanding of the principles and mechanisms of JS array sorting, and provide specific code examples. 1. Basic usage of sort() method

How to write a custom PHP array sorting algorithm? Bubble sort: Sorts an array by comparing and exchanging adjacent elements. Selection sort: Select the smallest or largest element each time and swap it with the current position. Insertion sort: Insert elements one by one into the sorted part.

The way to sort an array by value in PHP while preserving the key names is to use the usort() function to sort the array by value. Pass an anonymous function to the usort() function as a comparison function, which returns the difference in element values. usort() will sort the array according to the anonymous function while keeping the key names unchanged.

In PHP, use the uasort() function to sort an array according to custom sorting rules while retaining the original key names. A custom comparison function is a function that takes two elements as input and returns an integer: a negative number means the former is less than the latter, zero means they are equal, and a positive number means the former is greater than the latter.

The asort() function in PHP sorts an array by value. Specific code examples are required. PHP is a widely used server-side scripting language that has rich array processing functions. Among them, the asort() function is a very useful function, which can sort the array according to its value. This article will introduce the use of the asort() function in detail and give specific code examples. The function of the asort() function is to sort the array in ascending order by value while maintaining the association between keys and values. It is done by modifying the original number

PHP algorithm: How to use bubble sort to improve array sorting efficiency? Bubble sort is a simple but less efficient sorting algorithm, but we can improve the efficiency of bubble sort through some optimization strategies. This article will introduce how to use the bubble sort algorithm in PHP to optimize the sorting process of arrays, and provide specific code examples. The basic principle of bubble sorting is to start from the first element of the array each time and compare the sizes of two adjacent elements in sequence. If the previous element is greater than the following element, their positions are swapped. After such a round of comparison, the final

Tips for optimizing multi-dimensional array sorting in PHP: Create user-defined functions for sorting Use the array_multisort() function to apply multi-dimensional key reordering Practical case: Sorting products by array key-value pairs
