In JS Array (array) of the sort (sort) Note description

Source: Internet
Author: User
Tags sort

This article is mainly on the list of JS (array) of the sort (sort) Notes are described, the need for friends can come to the reference, I hope to help you

Look at the code directly, the test results are also posted inside the code as follows: var arrdemo = new Array ();  Arrdemo[0] = 10;  ARRDEMO[1] = 50;  Arrdemo[2] = 51;    ARRDEMO[3] = 100; Arrdemo.sort (); When the sort method is invoked, the array itself is changed, which affects the original array alert (Arrdemo),//10,100,50,51 By default the sort method is sorted in ASCII alphabetical order, rather than what we think is sorted by number size Arrdemo.sort ( function (a,b) {return a>b?1:-1});//Small to large sort alert (Arrdemo);//10,50,51,100 arrdemo.sort (function (a,b) {return a<b 1:-1});//from large to small sort alert (Arrdemo);//100,51,50,10 conclusion: 1. After the array calls the sort method, it affects itself (rather than generating a new array) the 2.sort () method is sorted by default, so the array of numeric   Order, do not assume that will be sorted by number size! 3. To change the default sort behavior (that is, sort by character), you can specify the collation function yourself (as shown in this example)

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.