JavaScript Basics--Array Method Chapter

Source: Internet
Author: User

1. Sort () method

1. Definition: Sorting elements of an array2The . Syntax: Arrayobject.sort (sortby) parameter is optional and must be a function3. Example: Instance 1: No parameter, the method by default converts an array element to a string, sorted by character encoding Order Instance 2: Incoming parametervararr =NewArray (5,2,4,1,3);functionSortbyargs (A, b) {returnAb; }varresult =Arr.sort (Sortbyargs); Console.log (Result)//Results: 1 2 3 4 5 (default small to large sort)Note: If you change the return value to B-A will be sorted from large to small (combined with the following to understand why)4principle: Parameters A and B are equivalent to removing the first two values of an array when called.The Sort method returns a value based on the passed-in parameter function.
Plus or minus or 0, to determine how the sort is similar to the bubbling sort, similar to bubbling sort .
default a-B < 0, A and B do not exchange positions (i.e. A is still before B);
A -B > 0 o'clock, A and B exchange positions (a after B);

To be continued ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~2018-03-28

JavaScript Basics--Array Method Chapter

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.