A summary of the JavaScript array sort () method

Source: Internet
Author: User
Tags array sort javascript array

Sort syntax: Arrayobject.sort (sortby), parameter SortBy Optional. Specifies the sort order. Must be a function.

Because the sort method is to compare array elements to strings first, and then to compare them according to the ASCII code of the first character of the string, it is not possible to satisfy the ordering requirements of our array of numeric sets, but the sort () method can use the function to sort the array by the bubbling method. My personal understanding is that if you want to sort it, the given function has the following two forms:

Array.Sort (function(b) {    A-b;})

This method is implemented from a small to a large sort.

Array.Sort (function(A, b  ) {return B-A;  })

This method can be implemented from a large to a small sort.

If you sort a multidimensional array, you should give the subscript of the sort.

In addition, JavaScript's sorting method also has the reverse () method, which can act as a reverse array.

A summary of the JavaScript array sort () method

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.