Home  >  Article  >  Web Front-end  >  How to use sort() in js

How to use sort() in js

小云云
小云云Original
2018-03-13 17:15:092087browse

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]

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]

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}

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}

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!

Statement:
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
Previous article:node file batch renameNext article:node file batch rename