Home > Web Front-end > JS Tutorial > body text

How to use sort() in js

小云云
Release: 2018-03-13 17:15:09
Original
2130 people have browsed it

If no parameters are used when calling this method, the elements in the array will be sorted alphabetically, or more precisely, in character encoding order. To achieve this, the elements of the array should first be converted to strings (if necessary) for comparison.

The array.sort() method defaults to ascending sorting. If you want to sort according to other criteria, you need to provide a comparison function. This function compares two values ​​and then returns a value to describe the two values. Numbers in relative order. The comparison function should have two parameters a and b, and its return value is as follows:

  • If a is less than b, a should appear before b in the sorted array, then return a value less than value of 0.

  • If a is equal to b, return 0.

  • If a is greater than b, return a value greater than 0.

Simple: compare the two parameters a and b of the function, return a-b in ascending order, and return b-a in descending order

Let’s understand it through a few examples

Ordinary array ascending order

var arr = [4,3,6,5,7,2,1];
arr.sort();
console.log(arr);//输出结果[1,2,3,4,5,6,7]
Copy after login

Ordinary data descending order

var arr = [4,3,6,5,7,2,1];
arr.sort();
arr.sort(function(a,b){    return b-a;
});
console.log(arr);//输出结果[7,6,5,4,3,2,1]
Copy after login

Object array sorting

var arr= [ 
    { 'sortNo': 2},
    { 'sortNo': 1},
    { 'sortNo': 5},
    { 'sortNo': 6},
    { 'sortNo': 7},
    { 'sortNo': 3},
    { 'sortNo': 9},
    { 'sortNo': 4},
    { 'sortNo': 0}
];
arr.sort(function(a, b){        return a.sortNo - b.sortNo;
});
console.log(arr);//输出结果//{ 'sortNo': 2}//{ 'sortNo': 1}//{ 'sortNo': 5}//{ 'sortNo': 6}//{ 'sortNo': 7}//{ 'sortNo': 3}//{ 'sortNo': 9}//{ 'sortNo': 4}//{ 'sortNo': 0}
Copy after login

Object array multi-condition sorting

(In this example, if the sortNo is the same, press sortNo2 from large to small)

var arr= [ 
    { 'sortNo': 2, 'sortNo2': 3},
    { 'sortNo': 1, 'sortNo2': 3},
    { 'sortNo': 5, 'sortNo2': 3},
    { 'sortNo': 6, 'sortNo2': 3},
    { 'sortNo': 7, 'sortNo2': 3},
    { 'sortNo': 3, 'sortNo2': 4},
    { 'sortNo': 3, 'sortNo2': 2},
    { 'sortNo': 3, 'sortNo2': 1},
    { 'sortNo': 3, 'sortNo2': 3},
    { 'sortNo': 8, 'sortNo2': 3},
    { 'sortNo': 4, 'sortNo2': 1},
    { 'sortNo': 4, 'sortNo2': 2}
];
arr.sort(function(a, b){    if (a.sortNo === b.sortNo) {            return b.sortNo2 - a.sortNo2;
    } else {            return a.sortNo - b.sortNo;
    }
});
console.log(arr); 

//输出结果//{ 'sortNo': 1, 'sortNo2': 3}//{ 'sortNo': 2, 'sortNo2': 3}//{ 'sortNo': 3, 'sortNo2': 4}//{ 'sortNo': 3, 'sortNo2': 3}//{ 'sortNo': 3, 'sortNo2': 2}//{ 'sortNo': 3, 'sortNo2': 1}//{ 'sortNo': 4, 'sortNo2': 2}//{ 'sortNo': 4, 'sortNo2': 1}//{ 'sortNo': 5, 'sortNo2': 3}//{ 'sortNo': 6, 'sortNo2': 3}//{ 'sortNo': 7, 'sortNo2': 3}//{ 'sortNo': 8, 'sortNo2': 3}
Copy after login

Related recommendations:

Detailed examples of JavaScript array sorting reverse() and sort() methods

How the function sort() works in javascript

JavaScript method to sort the elements of an array sort()

The above is the detailed content of How to use sort() in js. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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