The usage, parameters and sorting principle of the sort() method

The sort() method is used to sort the elements of an array and return an array. The default sort order is based on string Unicode code points.
Syntax: arrayObject.sort(sortby); parameter sortby is optional. Specifies the sort order. Must be a function.
Note: If this method is called with no parameters, the elements in the array will be sorted alphabetically, or more precisely, in the order of character encoding. To do this, first convert the elements of the array to strings (if necessary) so that they can be compared.

If you want to sort by other criteria, you need to provide a comparison function that compares two values ​​and returns a number that describes the relative order of the two values. The comparison function should have two parameters a and b, and its return value is as follows:
if a is less than b, and a should appear before b in the sorted array, return a value less than 0.
Returns 0 if a is equal to b.
If a is greater than b, a value greater than 0 is returned.

Example 1: We will create an array and sort it alphabetically:

<script type="text/javascript">

var arr = new Array(6)
arr[0] = "George"
arr[1] = "John"
arr[2] = "Thomas"
arr[3] = "James"
arr[4] = "Adrew"
arr[5] = "Martin"

document.write(arr + "<br />")
document.write(arr.sort())

</script>

output:

George, John, Thomas, James, Andrew, Martin
Andrew, George, James, John, Martin, Thomas

Example 2: We will create an array and sort it alphabetically:

<script type="text/javascript">

var arr = new Array(6)
arr[0] = "10"
arr[1] = "5"
arr[2] = "40"
arr[3] = "25"
arr[4] = "1000"
arr[5] = "1"

document.write(arr + "<br />")
document.write(arr.sort())

</script>

output:

10,5,40,25,1000,1
1,10,1000,25,40,5

Note that the above code does not sort the numbers by their size, to achieve this, a sorting function must be used:

<script type="text/javascript">

function sortNumber(a,b)
{
return a - b
}

var arr = new Array(6)
arr[0] = "10"
arr[1] = "5"
arr[2] = "40"
arr[3] = "25"
arr[4] = "1000"
arr[5] = "1"

document.write(arr + "<br />")
document.write(arr.sort(sortNumber))

</script>

Output:

10,5,40,25,1000,1
1,5,10,25,40,1000
Here it can be seen that if the installation is in ascending order, then the method is:

function sortNumber(a,b)
{
return a - b
}

In descending order it is:

function sortNumber(a,b)
{
return b - a
}

Of course, what if you want to sort by a property value in an array object?

The sort method receives a function as a parameter, where a layer of functions is nested to receive the object attribute name, and other parts of the code are the same as the normal use of the sort method.

var arr = [
    {name:'zopp',age:0},
    {name:'gpp',age:18},
    {name:'yjj',age:8}
];

function compare(property){
    return function(a,b){
        var value1 = a [property];
        var value2 = b [property];
        return value1 - value2;
    }
}
console.log(arr.sort(compare('age')))

The result is shown in the figure:


How to determine whether to sort in ascending or descending order according to different parameters?

sortBy: function(attr,rev){
    //The second parameter is not passed the default ascending order
    if(rev ==  undefined){
        rev = 1;
    }else{
        rev = (rev) ? 1 : -1;
    }
    return function(a,b){
        a = a[attr];
        b = b[attr];
        if(a < b){
            return rev * -1;
        }
        if(a > b){
            return rev * 1;
        }
        return 0;
    }
}

How to use:

newArray.sort(sortBy('number',false))


Reprinted from https://segmentfault.com/a/1190000000410506

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326162618&siteId=291194637