Array reordering method

Source: Internet
Author: User

Today in the process of review, found that the reordering method of the array sort () has been a misunderstanding, re-smooth, in this record ...

There are two methods in the array that can be used to reorder directly: reverse () and sort (), and the reverse () method is the order in which the array items are reversed. By default, the sort () method arranges array items in ascending order, and in this process the sort method invokes the ToString () transformation method of each array item, then compares the resulting string and then determines the sort. Have not noticed this before, always thought that the call sort () method will be directly to an array after the correct ordering, in fact, it is not the case, but the resulting string comparison, their understanding of it has been wrong, and today finally clear ...

The sort () method can accept a comparison function as a parameter so that we have a value that is in front of which value. The following is a simple comparison function.

function Compare (A, b) {

if (a<b) {

return-1;

}else if (a>b) {

return 1;

}else {

return 0;

}

}

Pass it to the sort method.

Array reordering method

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.