How to Use sort () in javascript _ javascript skills

Source: Internet
Author: User
The sort () method is mainly used to sort arrays. By default, this method converts array elements into strings and sorts them according to the ASC code. This is understandable, but if an array element is an Object, it cannot be converted into strings. Can it be sorted? The answer is no, so we will discuss in detail the syntax used by the sort () method: arrayObject. sort (sortby); The sortby parameter is optional. Specify the sorting order. Must be a function.

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

If no parameter is used when this method is called, the elements in the array are sorted alphabetically. More precise, the elements are sorted by character encoding. To achieve this,

First, the elements of the array should 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 returns a number indicating the relative sequence of the two values.

The comparison function should have two parameters, a and B. The returned values are as follows:

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

If a is equal to B, 0 is returned.

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

Sort numbers using the sort () method in js

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

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

The above Code does not sort numbers by the value size. To achieve this, you must use an ordering function:

Script var arr = [, 34,]; function sequence (a, B) {if (a> B) {return 1 ;} else if (
 
  

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

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

Script var arr = [,]; var arr2 = arr. sort (function (a, B) {if (a> B) {return 1;} else if (
   
    

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

It can also be simplified to this way.
Because: If a is less than B, a should appear before B in the sorted array, then a value smaller than 0 is returned.

If a is equal to B, 0 is returned.

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

 《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》

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

It is much easier to sort the relational alphabetic order. simply use the sort () method to complete the sorting:

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

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

Now, when I am studying javascript, I find that the sort () function is a bit strange (it may be my own level problem -_-!), So let's record what we found here. The parameter of the sort () method is very strange. It must be a function, but it is also an optional parameter. If there is no parameter, it will be arranged alphabetically by the string (even a value, will also be converted into strings for processing ). This parameter is used to compare the sizes of two values, for example:

The Code is as follows:


Function sortNumber (a, B ){
Return a-B; // The difference value is returned here. If it is a value smaller than 0, a will be placed in front. If it is greater than 0, B will be placed in front, if it is 0, you can just do it. (Bubble sorting method !!)
}

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


   
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.