How to use JS array sorting: sort() method for custom sorting
In JavaScript, arrays are a very common and important data type. When we need to sort the elements in an array, we can use the sort() method of the array. The sort() method sorts the array elements according to the default sorting rules, but sometimes we may need to customize the sorting of the array according to our own needs. This article will detail how to use the sort() method for custom sorting and provide specific code examples.
The sort() method is a native method of JavaScript array objects, and its function is to sort the array. The sort() method sorts the array elements in Unicode order, which is the default sorting rule. But when the elements in the array are strings, the sort() method does not sort according to our common dictionary order. In order to implement custom sorting, we can use a comparison function as a parameter of the sort() method.
The comparison function is used to define sorting rules. It accepts two parameters. We can customize the specific implementation logic of the comparison function. The comparison function needs to return a number that represents the size relationship between the two elements, thereby determining their position after sorting. If the return value is less than 0, the first element will be positioned earlier after sorting; if the return value is greater than 0, the first element will be positioned later after sorting; if the return value is equal to 0, the positions of the two elements will remain unchanged.
The following is a specific example of using the sort() method to customize sorting:
// 数组的原始顺序 var arr = [1, 4, 2, 5, 3]; // 使用自定义比较函数进行排序 arr.sort(function(a, b) { if(a < b) { return -1; // a在b前面 } else if(a > b) { return 1; // a在b后面 } else { return 0; // 位置不变 } }); // 输出排序后的数组 console.log(arr); // [1, 2, 3, 4, 5]
In the above code, we first define an array arr
, and directly The sort() method of the array is called. The parameter of the sort() method is an anonymous function, and this anonymous function is the comparison function we defined.
In the comparison function, we use conditional judgment statements to determine the size relationship between two elements. If the first element a
is less than the second element b
, then -1 is returned, indicating that a
should be in front of b
; if If a
is greater than b
, return 1, indicating that a
should be behind b
; if a
is equal to b
, return 0 means that the positions of the two elements remain unchanged.
After using a custom comparison function to sort the array, we can get the sorting results that meet our own needs.
In addition to sorting numeric type arrays, we can also customize string type arrays. The following is an example of custom sorting of a string array:
// 字符串数组的原始顺序 var arr = ["apple", "banana", "cat", "dog"]; // 使用自定义比较函数进行排序 arr.sort(function(a, b) { return a.localeCompare(b); }); // 输出排序后的数组 console.log(arr); // ["apple", "banana", "cat", "dog"]
In the above code, we use the localeCompare() method of strings to compare the size relationship of strings to achieve string sorting Custom sorting of arrays.
To summarize, the steps to use the sort() method for custom sorting are as follows:
Through the above example code, we can flexibly use the sort() method to implement custom sorting of the array according to our own needs.
The above is the detailed content of Custom sorting: Implementation method of sorting using JS array sort() method. For more information, please follow other related articles on the PHP Chinese website!