Home > Web Front-end > Front-end Q&A > Is sort sorting in es6?

Is sort sorting in es6?

WBOY
Release: 2022-04-25 15:30:10
Original
2624 people have browsed it

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))".

Is sort sorting in es6?

The operating environment of this tutorial: Windows 10 system, ECMAScript version 6.0, Dell G3 computer.

Is sort sorting in es6

sort sorting is in es6

sort() method is used to sort the elements of the array

Syntax

array.sort(callback(a,b))
Copy after login

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;
});
Copy after login

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 );
Copy after login

[Related recommendations:javascript video tutorialweb 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!

Related labels:
es6
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template