sort() Method is used to sort the elements of an array , And return the array . The default sort order is based on the string Unicode Code point .

grammar :array.sort(fun); parameter fun Optional . Specify the sort order . Must be a function .
notes : If the method is called without parameters , The elements in the array are sorted alphabetically , More precisely , Is sorted in the order of character encoding .

If you want to sort by other rules , You need to provide a comparison function , This function compares two values , It then returns a number indicating the relative order of the two values . The comparison function should have two parameters a and
b, The return value is as follows :
if a less than b, In sorted array a It should appear in b before , Returns a value less than 0 Value of .
if a be equal to b, Return to 0.
if a greater than b, Returns a value greater than 0 Value of .
It's easy : Compare two parameters of the function a and b, return a-b Ascending order , return b-a Descending order
// notes : The original array is changed

example :
1. No parameter transfer , Will not be sorted by value size , Sort by character encoding ;
var arr = ['General','Tom','Bob','John','Army']; var resArr = arr.sort();
console.log(resArr);// output ["Army", "Bob", "General", "John", "Tom"] var arr2 =
[30,10,111,35,1899,50,45]; var resArr2 = arr2.sort(); console.log(resArr2);// output
[10, 111, 1899, 30, 35, 45, 50]
2. Pass in parameters , Implement ascending order , Descending order ;
var arr3 = [30,10,111,35,1899,50,45]; arr3.sort(function(a,b){ return a - b;
}) console.log(arr3);// output [10, 30, 35, 45, 50, 111, 1899] var arr4 =
[30,10,111,35,1899,50,45]; arr4.sort(function(a,b){ return b - a; })
console.log(arr4);// output [1899, 111, 50, 45, 35, 30, 10]
3. Sort by a property value of an object in the array ;
var arr5 = [{id:10},{id:5},{id:6},{id:9},{id:2},{id:3}];
arr5.sort(function(a,b){ return a.id - b.id }) console.log(arr5); // Output new sort //
{id: 2} // {id: 3} // {id: 5} // {id: 6} // {id: 9} // {id: 10}
4. Sort by multiple attribute values of objects in the array , Multi conditional sorting ;
var arr6 =
[{id:10,age:2},{id:5,age:4},{id:6,age:10},{id:9,age:6},{id:2,age:8},{id:10,age:9}];
arr6.sort(function(a,b){ if(a.id === b.id){// If id identical , according to age In descending order of return b.age -
a.age }else{ return a.id - b.id } }) console.log(arr6); // Output new sort // {id: 2,
age: 8} // {id: 5, age: 4} // {id: 6, age: 10} // {id: 9, age: 6} // {id: 10,
age: 9} // {id: 10, age: 2}

Technology
©2019-2020 Toolsou All rights reserved,
Final review of database : Summary of comprehensive application questions Laplance operator ( Second derivative ) Simple learning of computer composition principle pyqt Button call python program _PyQt: Link button to function in program How much can you go up once you change jobs ? Today, I saw the ceiling of job hopping python in str Function usage _python in str Usage Summary of built-in functions MySQL trigger web The server nginx---linux Installation and deployment C++ Chapter V polymorphism exercises :( It's coming to an end )python Check built-in functions , How to check python Built in function