Use of sort (), parameters and Sorting principle (reprint)

Source: Internet
Author: User

sort()Method is used to sort the elements of an array and to return the arrays. The default sort order is based on the string Unicode code point.
Syntax: Arrayobject.sort (sortby), parameter SortBy Optional. Specifies the sort order. Must be a function.
Note: If the method is called without parameters , the elements in the array are sorted alphabetically, more precisely, by the order in which the characters are encoded . To do this, you should first convert the elements of the array to a string, if necessary, for comparison.

If you want to sort by other criteria, you need to provide a comparison function that compares two values and returns a number that describes the relative order of the two values. The comparison function should have two parameters A and B with the following return values:
If a is less than B, a value that is less than 0 is returned if a should appear before B in the sorted array.
If a equals B, 0 is returned.
If a is greater than B, a value greater than 0 is returned.

Example 1: We will create an array and sort alphabetically:

<ScriptType= "Text/javascript" >var arr = Span class= "Hljs-keyword" >new array (6) Arr[0] = " George "Arr[1] = " John "Arr[2" =  "Thomas" Arr[3] =  "James" Arr[4] =  "adrew" Arr[5] = " Martin "document.write (arr + " <br/> ") document.write (Arr.sort ()) </< Span class= "Hljs-name" >SCRIPT>            

Output:

George,John,Thomas,James,Adrew,MartinAdrew,George,James,John,Martin,Thomas

Example 2: We will create an array and sort alphabetically:

<Scripttype= "Text/javascript" >var arr = new array (6) arr[0] = " ten "Arr[1] =  "5" Arr[2] =  "Max" Arr[3] = " Arr[4] =  "1000 "Arr[5] = " 1 "document.write (arr +  "<br/>") document.write (Arr.sort ()) </ Script>               

Output:

10,5,40,25,1000,11,10,1000,25,40,5

Note that the above code does not sort numbers by numeric values, and to do this, you must use a sort function:

<ScriptType="Text/javascript" >functionSortnumber (b) {return a-B}var arr = new Array (6) arr[0] = "Ten" arr[1] = "5" arr[2] = "+" ar r[3] = "arr[4"= "$ " arr[5] = "1"document.write (arr + "<br/>")  document.write (Arr.sort (sortnumber))</script>        

Output:

10,5,40,25,1000,11,5,10,25,40,1000

As you can see here, if the installation is arranged in ascending order, then the method is:

function sortNumber(a,b){return a - b}

If it is in descending order:

function sortNumber(a,b){return b - a}

Of course, what if you want to sort by a property value in an array object?

sortThe method receives a function as a parameter, where a nested function is used to receive the object property name, and the rest of the code is the same as the normal use sort method.

var arr = [{Name' Zopp ',Age0}, {Name' GPP ',age:18}, {name: ' YJJ ', age:8}]; function compare (property" {return  function (a,b) {var value1 = a[< Span class= "Hljs-keyword" >property); var value2 = B[property return value1-value2; }}console.log (Arr.sort (compare (   

Results:

How do you decide whether to sort in ascending order or descending order, depending on the parameters?

SortBy:functionattrRev) {//The second parameter does not pass the default ascending orderIfRev = = undefined) {Rev =1else{rev = (rev)? 1:-1function (A,b) {a = A[attr] b = b[attr"; if (A < b) {return rev *-1;} if (A > b) {return rev * 10;}   

How to use:

newArray.sort(sortBy(‘number‘,false)) 



Reprinted from: 1190000000410506

Use of sort (), parameters and Sorting principle (reprint)

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.