Js_sort () sorting an array element

Source: Internet
Author: User
Tags sorts

Sort (), an array object that provides a sorting method.

This method receives a function that receives two parameters and sorts them by the judgment of the two parameters.

if the A-B > 0, then the A and B. swap positions. Of course, the words are in ascending order. If you want a descending row to change the A-B to b-a can be achieved, that is, a is greater than a, B and a exchange position, the large value row in front. The value of a, B is the comparison from arr[0],arr[1], similar to the bubbling sort.

1             //Sort (), the array provides a sorting method. 2             //This method receives a function that receives two parameters and sorts them by the judgment of the two parameters. 3             vararr = [5, 50, 20, 30, 47];4Arr.sort (function(A, b) {5                 returnAb;6             });7 Console.log (arr);8             /*if the A-B > 0, then the A and B. swap positions. Of course, the words are in ascending order. If you want a descending row, change the A-B to b-a.9 can be achieved, that is, B is greater than a, then B and a exchange position, the large value row front. The value of a, B is compared from arr[0],arr[1],Ten similar to bubble sort. */

Click here for the original

Js_sort () sorting an array element

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.