Examples of sorting JavaScript Object Arrays

Source: Internet
Author: User

Examples of sorting JavaScript Object Arrays

In javascript, multi-dimensional arrays and object arrays are sorted by native sort () methods.

The basic usage is not mentioned. Let's take a look at a simple sorting example:

//Sortalphabetically and ascending:varmyarray=["Bob","Bully","Amy"]myarray.sort()//Arraynow becomes ["Amy", "Bob", "Bully"]

After an array directly calls sort (), the array sorts the elements in the array in alphabetical order. More precise, the elements are sorted by character encoding.

Let's take a look at the sorting of numbers:

//Sortnumerically and ascending:varmyarray=[25, 8, 7, 41]myarray.sort(function(a,b){returna - b}) //Arraynow becomes [7, 8, 25, 41]

Sort (fun) accepts a sort rule function, which will compare the size of two numbers. In fact, our object array sorting works the same way.

For sorting object arrays, we first write a function that constructs a comparison function:

// The by function accepts a member name string as the parameter // returns a comparison function varby = function (name) that can be used to sort the array of objects containing the member) {returnfunction (o, p) {vara, B; if (typeofo = "object" & typeofp = "object" & o & p) {a = o [name]; B = p [name]; if (a = B) {return0;} if (typeofa = typeofb) {returna <B? -1: 1;} returntypeof a <typeofb? -1: 1 ;}else {throw ("error ");}}}

Array to be sorted:

varemployees=[]employees[0]={name:"George",age:32, retiredate:"March12, 2014"}employees[1]={name:"Edward",age:17, retiredate:"June2, 2023"}employees[2]={name:"Christine",age:58, retiredate:"December20, 2036"}employees[3]={name:"Sarah",age:62, retiredate:"April30, 2020"}

Directly call the function:

employees.sort(by("age"));

By now, object array sorting is basically implemented. How can we sort multiple key values? That is, sort the age first. If the age is the same, compare the name.
In this case, we can further modify the by function so that it can accept the second parameter. When the primary key value produces a match, the other compare method will be called to determine the upper limit.

// The by function accepts a member name string and an optional secondary comparison function as the parameter // and returns a comparison function that can be used to sort the array of objects containing the Member. // when o when [age] and p [age] are equal, secondary comparison functions are used to determine whether to compete with varby = function (name, minor) {returnfunction (o, p) {vara, B; if (o & p & typeofo = 'object' & typeofp = 'object') {a = o [name]; B = p [name]; if (a = B) {returntypeof minor = 'function '? Minor (o, p): 0;} if (typeofa = typeofb) {returna <B? -1:1;} returntypeof a <typeofb? -1: 1;} else {thro ("error") ;}} employees. sort (by ('age', by ('name ')));

Now you can use it with confidence. If you can't understand it, copy the by function to your application and call it directly.

The above section describes all the examples of JavaScript Object array sorting. I hope it will be helpful to you. If you have any questions, please leave a message, the editor will reply to you in a timely manner. Thank you very much for your support for the help House website!

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.