The sorting method of es6 array is "sort()". The sort() method is used to sort the elements of the array. The sorting order can be alphabetical or numerical, and in ascending or descending order. The default is alphabetical ascending order; this method has an optional parameter, which must be a function, and the syntax is "array. sort(callback(a,b))".
The operating environment of this tutorial: Windows 7 system, ECMAScript version 6, Dell G3 computer.
The sort() method is used to sort the elements of an array.
The sort order can be alphabetical or numerical, and in ascending or descending order.
The default sort order is ascending alphabetically.
Among them, the sort() method has an optional parameter. However, this parameter must be a function. When calling the sort() method of an array, if no parameters are passed, the elements in the array will be sorted in alphabetical order (character encoding order). If you want to sort according to other criteria, you need to pass a parameter and it is a function. This function Compares two values and returns a number describing the relative order of the two values.
Syntax:
array.sort(callback(a,b))
Parameters | Description |
---|---|
callback(a,b) | optional. Specifies the sort order. Must be a function. |
#Return value: Array type, which is a reference to the array. Please note that the array is sorted on the original array, no copy is made.
Example:
//sort的基本使用 let arr = [8, 1, 4, 3, 7, 9] let Arr = [21, 55, 29, 105, 45] console.log(arr.sort()) //[1, 3, 4, 7, 8, 9] console.log(Arr.sort()) // [105, 21, 29, 45, 55]
It can be seen from the above code that the sort() method can only correctly sort arrays within 0-9. Although the return value is given for array items with more than 100 digits, they are not the sorted results. This is because sort() performs internal sorting based on ASCLL codes, not based on numerical values. So this method cannot even perform formal sorting on numbers above two digits. How is it different from salted fish?
Here comes the key point: sort() can receive a callback (a, b) that carries two formal parameters, that is, a and b are two elements that are about to be compared in size, and there must be a return value.
When the return value of callback is a positive number, then b will be arranged before a;
When the return value of callback is a negative number , then a will be arranged before b;
When the return value of callback is 0, then the positions of a and b remain unchanged;
Every time sort is executed, the positions of the two parameters a and b in the original array will be exchanged based on the return value;
You will be confused after reading the above description, you must Will ask where is the return value? Who is the actual parameter of parameter a b? Once you understand the following code, these are all child’s play!
//sort 内部写法 let Arr = [56, 21, 29, 105, 45] Arr.sort(function(a, b) { //callback if (a > b) { // a b 分别是Arr中的 56 21 return 1 //返回正数 ,b排列在a之前 } else { return -1 //返回负数 ,a排列在b之前 } }) console.log(Arr) //[21, 29, 45, 55, 105]
Execution logic:
It should be noted that the two parameters received by callback(a, b) are a = > current item, b The next item of the current item, if the positions of the current item and the next item remain unchanged, b is the index of the next item -1; the condition for judging the end of the traversal is that the b parameter will end if it cannot obtain a value. For example, the third round in the above code When executing the second time, the index of the current item is 3, then b is the next item, that is, 4. The 4th item cannot be obtained in the array, and the conditions for continuing the traversal are not met, so the traversal ends!
Let’s talk about return values: The return values 1 and -1 written in the above code are just symbolic representations of 1 being a positive number and -1 being a negative number. No matter what return value you write in the code, sort will only judge you internally. Whether the return value is a positive number or a negative number, it is feasible to return 100 even if the equation is true or -10000 if it is not true.
Explanation of abbreviation:
//简写 最终版 let Arr = [56, 21, 88, 10, 5, 77] Arr.sort((a, b) => a - b) //箭头函数不加大括号指向这个函数的返回值,可以不写return关键字 console.log(Arr) //[5, 10, 21, 56, 77, 88]
As can be seen from the above figure, the internal processing method of the callback function is a - b, instead of comparing two numbers. . This is because the step of comparing two numbers is done by sort. You only need to specify the return value. Mathematically, it happens that large numbers - decimals = positive numbers, decimals - large numbers = negative numbers
Example If 56 - 21 = 35 is a positive number, the return value is a positive number, and the positive number represents changing the position;
21 - 88 = 35 is a negative number, the return value is a negative number, and the negative number represents changing the position;
If in mathematics, large number - small number ≠ positive number, small number - large number ≠ negative number, it cannot be abbreviated like this. So it should be clear that sort internally compares each other rather than subtracts each other;
[Related recommendations: javascript video tutorial, web front-end]
The above is the detailed content of What is the sorting method of es6 array. For more information, please follow other related articles on the PHP Chinese website!

在es6中,可以利用“Array.isArray()”方法判断对象是否为数组,若判断的对象是数组,返回的结果是true,若判断对象不是数组,返回的结果是false,语法为“Array.isArray(需要检测的js对象)”。

es6中遍历跟迭代的区别是:遍历强调的是要把整个数据依次全部取出来,是访问数据结构的所有元素;而迭代虽然也是依次取出数据,但是并不保证取多少,也不保证把所有的数据取完,是遍历的一种形式。

在es6中,可用Object对象的is()方法来判断两个对象是否相等,该方法检测两个变量的值是否为同一个值,判断两个对象的引用地址是否一致,语法“Object.is(对象1,对象2)”;该方法会返回布尔值,若返回true则表示两个对象相等。

转换方法:1、利用“+”给数字拼接一个空字符,语法“数字+""”;2、使用String(),可把对象的值转换为字符串,语法“String(数字对象)”;3、用toString(),可返回数字的字符串表示,语法“数字.toString()”。

在es6中,assign用于对象的合并,可以将源对象的所有可枚举属性复制到目标对象;若目标对象与源对象有同名属性,或多个源对象有同名属性,则后面的属性会覆盖前面的属性,语法为“Object.assign(...)”

改变方法:1、利用splice()方法修改,该方法可以直接修改原数组的内容,语法为“数组.splice(开始位置,修改个数,修改后的值)”;2、利用下标访问数组元素,并重新赋值来修改数组数据,语法为“数组[下标值]=修改后的值;”。

sort排序是es6中的;sort排序是es6中用于对数组的元素进行排序的方法,该方法默认不传参,按照字符编码顺序进行排序,排序顺序可以是字母或数字,并按升序或降序,语法为“array.sort(callback(a,b))”。

在es6中,import as用于将若干export导出的内容组合成一个对象返回;ES6的模块化分为导出与导入两个模块,该方法能够将所有的导出内容包裹到指定对象中,语法为“import * as 对象 from ...”。


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

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Linux new version
SublimeText3 Linux latest version

SublimeText3 Chinese version
Chinese version, very easy to use

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

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