JavaScript the sort () method uses the detailed _javascript technique

Source: Internet
Author: User
Tags numeric value

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

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

If the method is invoked without parameters, the elements in the array are sorted alphabetically, and more precisely, sorted in the order of character encoding. To achieve this,

You should first convert the elements of an 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 then returns a number that describes the relative order of the two values.

The comparison function should have two parameters A and B, and the return value is as follows:

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

If a equals B, it returns 0.

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

Sort the numbers using the sort () method in JS

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

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

The code above does not sort the numbers by the size of the numeric value, 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 (a,b) {
    if (a>b) {return
      1;
    } else if (a<b) {
      return-1
    }else{return
      0
    }
  }
  Console.log (Arr.sort (sequence));
</script>

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

Of course, you can also write the sort function to 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>

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

It can be simplified as such.
Because: If a is less than B, in the sorted array, a should appear before B, and a value less than 0 is returned.

If a equals B, it returns 0.

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>

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

It is much simpler to sort the relationships alphabetically, and the sort () method is OK:

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

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

Now in learning JavaScript, finding the sort () function is a bit strange (probably my level of problem-_-! , so just record what you found here. Sort () The parameter of this method is very strange, must be a function, but also optional argument, if there is no argument, it will default to the dictionary order of the string (even if the value will be converted to a string to handle). This parameter is to be able to compare the size of two values, such as:

Copy Code code as follows:

function Sortnumber (A, b) {
return a-b; Here is the return of their difference, if it is less than 0 of the value, will be a row in front, if more than 0, will be b in front, if it is 0, just casually. (Bubble Sort Method!!) )
}

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

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

Then the arrangement of the original 10,5,4 will become 4,5,10. Here is a description of the process, clearly sortnumber should have two parameters, but we call when there is no parameter, how to compare ah? Here's the thing, when Arr calls sort from the first number, 10 is preceded by no number compared to it, so to the second, 5, 10 is compared to 5, and then the Sortnumber is called and the 10 and 5 are passed in, which is the property of sort ().

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.