JS array sorting function sort () Introduction

Source: Internet
Author: User
Tags array sort arrays comparison sort

This article gives us a simple discussion of the next JS array sorting function sort () usage and examples, the need for small partners can refer to.

JavaScript implements multidimensional array, object array ordering, its practical is the native sort () method, for the elements of the array to sort.

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

Arrayobject.sort (SortBy)

The value returned 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 invoked without parameters, the elements in the array are sorted alphabetically, and more precisely, sorted in the order of character encoding. To do this, you should first convert the elements of the array into strings (if necessary) for comparison.

If you want to sort by other criteria, you need to provide a comparison function that compares two values, and then returns a number that describes the relative order of the two values. The comparison function should have two parameters A and B, and the return value is as follows:

If a is less than B, a value less than 0 is returned in the sorted array before a should appear in B.

If a equals B, it returns 0.

If a is greater than B, a value greater than 0 is returned.

?

1 2 3 4 5 6 7 8 9 10 11 12-13 function Numascsort (a,b) {return a-b} function Numdescsort (a,b) {return b-a;} var arr = new Array (3600, 5010, 10 100, 801); Arr.sort (Numdescsort); Alert (arr); Arr.sort (Numascsort); Alert (arr);

Sort (fun) accepts a collation function that compares the size of 2 digits. And our array of objects, in fact, is the same principle.

If you do not compare the size of a number, you can do this:

?

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

Arrays call sort () directly, the arrays are sorted alphabetically by the elements in the array, and more precisely, sorted in the order of character encoding.

For object array ordering, we first write a function that constructs the comparison function:

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20-21 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 that contains 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) {Retu RN 0; } if (typeof a = = typeof b) {return a < B. -1:1} return typeof a < typeof B? -1:1; else {throw ("error");} }

The array to sort:

?

1 2 3 4 5 var employees=[] Employees[0]={name: "George", age:32, Retiredate: "March, 2014"} 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"}

Call function directly:

?

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

Here, the object array sort is basically implemented. So how do you sort multiple key values? The meaning is to sort the age first, and then compare name if the age is the same.

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, another compare method will be invoked to decide the difference.

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22-23 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 sorted by an array of objects that contains the member//when O[age] and p[age] are equal, the secondary comparison function is used to select the top var by = function ( Name,minor) {return function (o,p) {var a,b; if (o && p && typeof o = = ' object ' && typeof p = = ' obj ECT ') {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 '));

OK, now you can use it with ease. If you do not understand, you can directly copy this by function to your application, direct call can be.

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.