The sort () method in JavaScript

Source: Internet
Author: User

The sort () method is primarily used for sorting arrays, which, by default, converts an array element to a string and then sorts it by ASC code, which everyone understands, but what if the array element is an object and cannot be sorted by the string? The answer, of course, is no, so let's explore the use of the sort () method in detail.

Syntax:arrayobject.sort (sortby), parameter SortBy Optional. Specifies the sort order. Must be a function.

The sort () method is used to sort elements of an array. If the method is called without parameters, the elements in the array are sorted alphabetically, more precisely, by the order in which the characters are encoded. To do this, you should first convert the elements of the array to a string, if necessary, for comparison. If you want to sort by other criteria, you need to provide a comparison function that 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 with the following return values:

If a is less than B, a value that is less than 0 is returned if a should appear before B in the sorted array.

If a equals B, 0 is returned.

If a is greater than B, a value greater than 0 is returned.

Sort numbers by using the sort () method in JS

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

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

The above code does not sort the numbers by the size of the numeric values, and to do this, you must use a sort function:

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

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

Of course, you can also write the sort function to the order in the sort () method:

<script>  var arr = [23,12,1,34,116,8,18,37,56,50];   var arr2 = Arr.sort (function(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)

It can also be simplified into such a notation:
Because: If a is less than B, a value that is less than 0 is returned if a should appear before B in the sorted array.

If a equals B, 0 is returned.

If a is greater than B, a value greater than 0 is returned, similar to the bubbling sort method.

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

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

Principle Analysis:

<script type= "Text/javascript" >    function Sortnumber (b) {return A- b}     var New Array (3)    arr[0] = "ten";    arr[1] = "5";    arr[2] = "4";     + "<br/>");    document.write (Arr.sort (Sortnumber)); </script>

The arrangement of the original 10,5,4 will become 4,5,10. Here is a description of the process, obviously sortnumber should have two parameters, but we call when a parameter is not, how to compare ah? Here is the case when Arr calls sort from the first number, 10 is no number compared to it, so the second, that is 5, then 10 will be compared with 5, so it will call Sortnumber and 10 and 5 in, this is the character of sort ().

It's a lot easier to sort alphabetically, and it's OK to just use the sort () method:

<script>  var arr = [' Fanda ', ' banner ', ' Find ', ' zoom ', ' index ', ' width ', ' JavaScript '];  Console.log (Arr.sort ()); </script>

return: ["banner", "Fanda", "Find", "index", "JavaScript", "width", "Zoom"]

The sort () method in JavaScript

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.