Home>Article>Web Front-end> Is sort sorting in es6?
sort sorting is in es6; sort sorting is a method used in es6 to sort the elements of an array. This method does not pass parameters by default and sorts according to the character encoding order. The sorting order can be letters or numbers. , and in ascending or descending order, the syntax is "array.sort(callback(a,b))".
The operating environment of this tutorial: Windows 10 system, ECMAScript version 6.0, Dell G3 computer.
sort sorting is in es6
sort() method is used to sort the elements of the array
Syntax
array.sort(callback(a,b))
No parameters are passed by default, and they will be sorted according to the character encoding order
//Passing parameters: The parameter is a function, and the function has two parameters (a, b)
// a > b, return a positive number
// a = b, return 0
// a < b, return a negative number, after re In the sorted array, a is before b
// a - b , the returned array is sorted from small to large
// b - a , the returned array is sorted from large to small Sorted
let sortArr = [ {a:1}, {a:3}, {a:-1}, ] let newSortArr = sortArr.sort((r1,r2)=>{ let r = r1.a-r2.a; console.log(r) return r; });
The sorting order can be letters or numbers, and in ascending or descending order. The default is ascending alphabetical order;
is as follows:
var arr = new Array("orange", "mango", "banana", "sugar"); var sorted = arr.sort(); console.log("Returned string is : " + sorted );
[Related recommendations:javascript video tutorial、web front-end】
The above is the detailed content of Is sort sorting in es6?. For more information, please follow other related articles on the PHP Chinese website!