JS Array sort Comparison Function and Principle

Keywords: Javascript less encoding

// Arrangement from large to small
var arr=[1,2,4,456,7,67,3]; 
arr.sort(function(a,b){ 
return a-b; 
}); 
console.log(arr);//[1, 2, 3, 4, 7, 67, 456]

//Case-insensitive arrangement of letters from small to large
var arr1=['B','a','b','A'];
arr1.sort(function(a,b){
    if(a.toString().toLowerCase()>b.toString().toLowerCase())
        return 1;
    return -1;
})
console.log(arr1);//["a", "A", "B", "b"]

//Make the elements of an array in odd-numbered and even-numbered order
var arr3=[1,8,9,4,5,6,7,2,3];
arr3.sort(function(a,b){
    //Firstly, it is judged that satisfying a is even number b is odd number.
    if(a%2==0 && b%2==1){
        return 1;
    }
    //Then we judge that satisfying a B is odd or even, and a > b, so that we can arrange it from small to large.
    if(a>b && (a%2==1 && b%2==1 || a%2==0 && b%2==0)){
        return 1;
    }
    return -1;
})
console.log(arr3);//[1, 3, 5, 7, 9, 2, 4, 6, 8];

Definition and usage:

The sort() method is used to sort the elements of an array.

Syntax:

arrayObject.sort(sortby)

Note: sortby must be a function that specifies the sort order. Optional parameters

Return value:

A reference to an array. Note that arrays are sorted on the original array and no replicas are generated.

Description and Principle:

If the method is called without parameters, the elements in the array will be sorted alphabetically, more precisely, in the order of character encoding. To achieve this, the elements of the array should first be converted into strings (if necessary) for comparison.

If you want to sort by other criteria, you need to provide a comparison function that compares two values and then returns a number that explains 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, A should appear before B in the sorted array and return a value less than 0.
If a equals b, return 0.
If A is greater than b, a value greater than 0 is returned.

Example:

Example 1: In this case, we will create an array and sort it alphabetically

  1. <script type="text/javascript">  
  2.  var arr = new Array(6)  
  3.  arr[0] = "George"  
  4.  arr[1] = "John"  
  5.  arr[2] = "Thomas"  
  6.  arr[3] = "James"  
  7.  arr[4] = "Adrew"  
  8.  arr[5] = "Martin"  
  9.   
  10.  document.write(arr + "<br />")  
  11.  document.write(arr.sort())  
  12. </script>  
Output:

George,John,Thomas,James,Adrew,Martin
Adrew,George,James,John,Martin,Thomas


Example two:

In this case, we will create an array and sort it alphabetically:

  1. <script type="text/javascript">  
  2.     var arr = new Array(6)  
  3.     arr[0] = "8"  
  4.     arr[1] = "16"  
  5.     arr[2] = "50"  
  6.     arr[3] = "6"  
  7.     arr[4] = "100"  
  8.     arr[5] = "1"  
  9.   
  10.     document.write(arr + "<br />")  
  11.     document.write(arr.sort())  
  12. </script>  

Output:

8,16,50,6,100,1
1,100,16,50,6,8

Note that the above code does not sort the numbers by the size of the values. To achieve this, you must use a sort function:

(a-b output sort from small to large, b-a output sort from large to small)

  1. <script type="text/javascript">  
  2.     function sortNumber(a,b){  
  3.         return a - b  
  4.     }  
  5.     var arr = new Array(6)  
  6.         arr[0] = "8"  
  7.         arr[1] = "16"  
  8.         arr[2] = "50"  
  9.             arr[3] = "6"  
  10.         arr[4] = "100"  
  11.         arr[5] = "1"  
  12.   
  13.        document.write(arr + "<br />")  
  14.        document.write(arr.sort(sortNumber))  
  15. </script>  

Output:

8,16,50,6,100,1
1,6,8,16,50,100

The sorting idea should be similar to the bubble sorting idea. By comparing the two adjacent numbers, such as 80 and 16, 80 is used as a method function sortNum(a,b) parameter list, A and 16 are used as b, and returned a - B through the statement; a value is returned, 80 - 16 = 64, if the return value >= 1, it means that a appears after B in the sorted sequence, so 80 will appear after sorting. After 16, at this time, the new data sequence is 16, 80, 50, 6, 100, 1, and then 80 as a, 50 as B. After the first round, 100 will be in the last place, and a new round of sorting will start, which is similar to the idea of bubble sorting.


Writing of Random Arrangement:

  1. <script type="text/javascript">  
  2.     function sortRandom(){  
  3.         return Math.random()<0.5?1:-1;  
  4.     }  
  5.   
  6.     var arr = new Array(6)  
  7.         arr[0] = "8"  
  8.         arr[1] = "16"  
  9.         arr[2] = "50"  
  10.         arr[3] = "6"  
  11.         arr[4] = "100"  
  12.         arr[5] = "1"  
  13.   
  14.         document.write(arr + "<br />")  
  15.         document.write(arr.sort(sortRandom))  
  16. </script>  

Copyright Statement: This article is the original article of the blogger. It can not be reproduced without the permission of the blogger. https://blog.csdn.net/sinat_32546159/article/details/61618623



Posted by bullbreed on Sat, 11 May 2019 08:04:37 -0700