Article directory
1 sort function
The sort() method is used to sort the elements of the array and return the array.
默认排序顺序是在将元素转换为字符串
, and then compare the sequence of points when their UTF-16 代码
cell values were constructed.
2 try
const months = ['March', 'Jan', 'Feb', 'Dec'];
months.sort();
console.log(months); // ["Dec", "Feb", "Jan", "March"]
const arr = [1, 30, 4, 21, 100000];
arr.sort();
console.log(arr); // [1, 100000, 21, 30, 4]
3 Syntax & Parameters & Return Value
3.1 Grammar
Generally, it is more convenient to pass in one 比较函数
, and the code is more readable.
// 无函数
sort()
// 箭头函数
sort((a, b) => {
/* … */ } )
// 比较函数
sort(compareFn)
// 内联比较函数
sort(function compareFn(a, b) {
/* … */ })
3.2 Parameters
compareFn
Used to specify 按某种顺序进行排列的函数
. If omitted, the elements are sorted by the Unicode position of each character of the converted string.
a the first element to be compared
b the second element to compare
3.3 return value
The sorted array, ie 会改变的原数组
.
4 Description and examples
If not specified compareFn
, the element is converted to 字符串的诸个字符的 Unicode 点位进行排序
.
For example, "Banana" would be sorted before "cherry".
When numbers are sorted from smallest to largest, 9 appears before 80, but because (unspecified compareFn
), compared numbers are converted to strings first, "80" comes before "9" in Unicode order .
If specified compareFn
, the array will be sorted by the return value of the function call. That is, a and b are the two elements to be compared:
- If
compareFn(a, b)
greater than 0, b will be sorted before a. - If
compareFn(a, b)
is less than 0, then a will be arranged before b; - If
compareFn(a, b)
equal to 0, the relative positions of a and b remain unchanged. Note: The ECMAScript standard does not guarantee this behavior, and not all browsers will comply (e.g. Mozilla pre-2003); compareFn(a, b)
Must always return the same comparison result for the same input, otherwise the result of the sort will be undefined.
compareFn(a, b) return value |
sort order |
---|---|
> 0 |
a after b |
< 0 |
a before b |
=== 0 |
preserve the order of a and b |
4.1 The format of the comparison function is as follows
function compareFn(a, b) {
if (在某些排序规则中,a 小于 b) {
return -1;
}
if (在这一排序规则下,a 大于 b) {
return 1;
}
// a 一定等于 b
return 0;
}
To compare numbers instead of strings, the comparison function can simply be a
subtracted b
, and the following function will sort the array in ascending order (if it does not contain Infinity
and NaN
):
function compareNumbers(a, b) {
return a - b;
}
4.2 Function expressions are also convenient
const numbers = [4, 2, 5, 1, 3];
numbers.sort(function (a, b) {
return a - b;
});
console.log(numbers); // [1, 2, 3, 4, 5]
const numbers2 = [4, 2, 5, 1, 3];
numbers2.sort((a, b) => a - b);
console.log(numbers2); // [1, 2, 3, 4, 5]
5 Objects can be sorted according to a certain attribute order
Example:
const items = [
{
name: 'Edward', value: 21 },
{
name: 'Sharpe', value: 37 },
{
name: 'And', value: 45 },
{
name: 'The', value: -12 },
{
name: 'Magnetic', value: 13 },
{
name: 'Zeros', value: 37 }
];
//按 value 排序
items.sort((a, b) => a.value - b.value);
//按 name 排序
items.sort((a, b) => {
const nameA = a.name.toUpperCase(); // 不区分大小写
const nameB = b.name.toUpperCase(); // 不区分大小写
if (nameA < nameB) {
return -1;
}
if (nameA > nameB) {
return 1;
}
// name 必须相等
return 0;
});