Home  >  Article  >  Web Front-end  >  Detailed explanation of the use of sort() method in javascript_javascript skills

Detailed explanation of the use of sort() method in javascript_javascript skills

WBOY
WBOYOriginal
2016-05-16 15:41:321434browse

Syntax: arrayObject.sort(sortby); the parameter sortby is optional. Specifies the sort order. Must be a function.

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

If this method is called without parameters, the elements in the array will be sorted alphabetically, or more precisely, in character encoding order. To achieve this,

First convert the elements of the array into strings (if necessary) for comparison.

If you want to sort by other criteria, you need to provide a comparison function, which 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, a should appear before b in the sorted array, then return a value less than 0.

If a is equal to b, return 0.

If a is greater than b, return a value greater than 0.

Use the sort() method in js to sort numbers

<script>
  var arr = [23,12,1,34,116,8,18,37,56,50];
  alert(arr.sort();
</script>

Return: [1, 116, 12, 18, 23, 34, 37, 50, 56, 8]

The above code does not sort the numbers according to their size. To achieve this, a sorting function must be used:

<script>
  var arr = [23,12,1,34,116,8,18,37,56,50];
  function sequence(a,b){
    if (a>b) {
      return 1;
    }else if(a<b){
      return -1
    }else{
      return 0;
    }
  }
  console.log(arr.sort(sequence));
</script>

Return: [1, 8, 12, 18, 23, 34, 37, 50, 56, 116] (no problem)

Of course, you can also write the sorting function into the sort() method:

<script>
  var arr = [23,12,1,34,116,8,18,37,56,50];
  var arr2 = arr.sort(function(a,b){
     if (a>b) {
      return 1;
    }else if(a<b){
      return -1
    }else{
      return 0;
    }  
  })
  console.log(arr2);
</script>

Return: [1, 8, 12, 18, 23, 34, 37, 50, 56, 116] (no problem either)

can also be simplified to this way of writing
Because: if a is less than b, a should appear before b in the sorted array, then a value less than 0 is returned.

If a is equal to b, return 0.

If a is greater than b, return a value greater than 0

 <script>
   var arr = [23,12,1,34,116,8,18,37,56,50];
   function sequence(a,b){
     return a - b;
   }
   console.log(arr.sort(sequence));
 </script>

Returns: [1, 8, 12, 18, 23, 34, 37, 50, 56, 116] (also correct)

Sorting in alphabetical order is much simpler, just use the sort() method directly:

 <script>
   var arr = ['fanda','banner','find','zoom','index','width','javascript'];
   console.log(arr.sort());
 </script>

Return: ["banner", "fanda", "find", "index", "javascript", "width", "zoom"]

Now when I am learning javascript, I found that the sort() function is a bit strange (maybe it is a problem with my level -_-!), so I will record what I found here. The parameters of the sort() method are very strange. They must be functions, but they are also optional parameters. If there are no parameters, they will be arranged in dictionary order of strings by default (even numerical values ​​will be converted into strings for processing. ). This parameter is to be able to compare the size of two values, such as:

Copy code The code is as follows:

function sortNumber(a, b){
return a - b; //What is returned here is their difference. If it is less than 0, a will be ranked in front. If it is greater than 0, b will be ranked in front. If it is 0, it will be whatever. . (Bubble sorting method!!)
}

The application is as follows (this example is so classic!!):

<script type="text/javascript">
function sortNumber(a,b){return a - b}
var arr = new Array(3)
arr[0] = "10";
arr[1] = "5";
arr[2] = "4";
document.write(arr + "<br />");
document.write(arr.sort(sortNumber));
</script>

Then the original arrangement of 10,5,4 will become 4,5,10. Here is an explanation of this process. Obviously sortNumber should have two parameters, but when we call it, there is no parameter at all. Why? Compare? This is like this. When arr calls sort starting from the first number, there is no number before 10 to compare with it, so it goes to the second number, which is 5. At this time, 10 will be compared with 5, so it will be called sortNumber and pass 10 and 5 in, this is the nature of sort().

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn