I. How sort in JavaScript sorts the data
sort()method to sort the elements of the array in situ and return the arrays. The sort may not be stable. The default is sorted by the Unicode code point of the string;
Syntax: Arr.sort ([comparefunction])
Parameter comparefunction
Optional. Used to specify functions that are arranged in some order. If omitted, the element is sorted according to the Unicode bit points of the characters converted to the string.
If Comparefunction (A, b) is less than 0, then A is arranged before B;
If Comparefunction (A, B) equals 0, the relative positions of A and B are unchanged. Note: The ECMAScript standard does not guarantee this behavior, and not all browsers will follow
If Comparefunction (A, b) is greater than 0, B is arranged before a.
1 //The elements in the array are arranged in small order 2 var arr=[11,55,22,45,16,87];3 4 arr.sort (function (a, b) {5 return a- }); 7 Console.log (arr);
Second, the principle of analog JavaScript internal data sequencing
1 sortself (Arr,function(A, b) {2 Return a- console.log (arr); 5 6 function Sortself (ARRAY,FN) {7 for (var i = 0; i < array.length-1; i++) {8 var issorted=true;//default already ordered 9 for (Var j = 0; J < Array.length-1-i; J + +) {10//Call function one-by-one if (FN (array[j],array[j+1]) >0) {12//Exchange two variable var temp=array[j];14 Array[j]=array[j +1];15 array[j+1]=temp;16 issorted=false }19 if(issorted) {break }23}
To simulate sort sorting in JavaScript