1. Sort () method
1. Definition: Sorting elements of an array2The . Syntax: Arrayobject.sort (sortby) parameter is optional and must be a function3. Example: Instance 1: No parameter, the method by default converts an array element to a string, sorted by character encoding Order Instance 2: Incoming parametervararr =NewArray (5,2,4,1,3);functionSortbyargs (A, b) {returnAb; }varresult =Arr.sort (Sortbyargs); Console.log (Result)//Results: 1 2 3 4 5 (default small to large sort)Note: If you change the return value to B-A will be sorted from large to small (combined with the following to understand why)4principle: Parameters A and B are equivalent to removing the first two values of an array when called.The Sort method returns a value based on the passed-in parameter function.
Plus or minus or 0, to determine how the sort is similar to the bubbling sort, similar to bubbling sort .
default a-B < 0, A and B do not exchange positions (i.e. A is still before B);
A -B > 0 o'clock, A and B exchange positions (a after B);
To be continued ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~2018-03-28
JavaScript Basics--Array Method Chapter