The array sort function in JS sort ()

Source: Internet
Author: User
Tags array sort

JavaScript implements a multidimensional array, an array of objects, and its utility is the native sort () method, which is used to sort the elements of the array.

The sort () method is used to sort elements of an array. The syntax is as follows:

Arrayobject.sort (SortBy)

The return value is a reference to the array. Note that the array is sorted on the original array, and No replicas are generated .

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.

Ex

var array1 = [0,1,5,10,15];

Array1.sort ();//The result is: 0,1,10,15,5

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.

function Numascsort (A, b) {return a-A;} function Numdescsort (A, b) {return b-a;} var arr = new Array (3600, 5010, 10100, 801); Arr.sort (Numdescsort); alert (arr); Arr.sort (numascsort); alert (arr);

Sort (fun) accepts a collation function that compares the size of 2 numbers. And our array of objects is actually the same principle.
If you do not compare the size of the numbers, you can do this:

var myarray=["Apple", "Banana", "Orange"]myarray.sort ()

After the array calls the sort () directly, the arrays are sorted alphabetically by the elements in the array, more precisely, in the order in which the characters are encoded.
For object array ordering, we first write a function that constructs a comparison function:

The by function takes a member name string as a parameter//and returns a comparison function that can be used to sort the array of objects containing the member var by = function (name) {return function (O, p) {   var a, b;   if (typeof o = = = "Object" && typeof p = = = = "Object" && o && p) {     a = O[name];     b = P[name];     if (a = = b) {       return 0;     }     if (typeof a = = typeof b) {       return a < b? -1:1;     }     Return typeof a < typeof B? -1:1;   }   else {     throw ("Error");}}}   

Array to sort on:

var employees=[]employees[0]={name: "George", age:32, Retiredate: "March,"}employees[1]={name: "Edward", age:17 , Retiredate: "June 2, 2023"}employees[2]={name: "Christine", age:58, Retiredate: "December, 2036"}employees[3]={name : "Sarah", age:62, Retiredate: "April 30, 2020"}

Calling functions directly:

Employees.sort (by ("Age"));

Here, the object array sort is basically implemented. How do you implement multiple key-value sequencing? The meaning is to sort the age first, if age is the same, then compare name.
At this point, we can further modify the by function so that it can accept the second parameter, and when the primary key value produces a match, the other compare method will be called to decide to compete.

The by function takes a member name string and an optional secondary comparison function as a parameter//and returns a comparison function that can be used to sort the array of objects that contains the member///when O[age] and p[age] are equal, the secondary comparison function is used to decide the var by = function ( Name,minor) {return function (o,p) {   var a, b;   if (o && p && typeof o = = = ' object ' && typeof p = = = ' object ') {     a = O[name];     b = P[name];     if (a = = b) {       return typeof minor = = = ' function '? minor (O,p): 0;     }     if (typeof a = = typeof b) {       return a < b? -1:1;     }     Return typeof a < typeof B? -1:1;   } else{     thro ("Error"),}}}   Employees.sort (by (' Age ', by (' name '));

  

The array sort function in JS sort ()

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.