Sort JavaScript Array objects

Source: Internet
Author: User
Tags array sort javascript array

Sort JavaScript Array objects

JavaScript array built-in sorting function JavaScript built-in sort function is a collection of multiple sorting algorithms JavaScript implements multidimensional arrays, object array ordering, and its practical is the native sort () method, which is used to sort the elements of an array. The sort () method is used to sort elements of an array.  The syntax is as follows: Arrayobject.sort (order); Test A:varmm=[1,4,0,6,3,1,2];mm.sort (); alert (mm);//0 1 1 2 3 4 6The 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. 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, whose return value is as follows: If A is less than B, a should appear before B in the sorted array, then return a less than0the value. If a equals B, it returns0. A, B is considered equal if a is greater than0the value. b should be in the back of a to test B:functionNumascsort (A, b) {returnA-b;}functionNumdescsort (A, b) {returnB-A;}vararr =NewArray (1, 2, 3, 4); Arr.sort (Numdescsort); alert (arr);//4 3 2 1Arr.sort (numascsort); alert (arr);//1 2 3 4Compare character array Test C:varmyarray=["Apple", "Banana", "Orange"]myarray.sort () alert (myarray);//Apple Banana Orangeafter the array calls the sort () directly, the arrays are sorted alphabetically by the elements in the array, to be more precise, to sort by the order of the character encodings for the object array ordering, we first write a function to construct the 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 membervarby =function(name) {return function(o, p) {varA, B;if(typeofo = = = "Object" &&typeofp = = = "Object" && o &&FP) {a=o[name];b=P[name];if(A = = b) {return0;}if(typeofA = = =typeofb) {returnA < b? -1:1;}return typeofA <typeofB? -1:1;}Else{Throw("Error"); }}}varemployees=[]employees[0]={name: "George", age:32, Retiredate: "March 12, 2014"}employees[1]={name: "Edward", Age:17, Retiredate: "June 2, 2023"}employees[2]={name: "Christine", age:58, Retiredate: "December 20, 2036"}employees[3]={name: "Sarah", age:62, Retiredate: "April 30, 2020"}employees.sort (by ("Age") ; alert (employees); 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 contain the member//when O[age] and p[age] are equal, the secondary comparison function is used tovarby =function(Name,minor) {return function(o, p) {varA, B;if(typeofo = = = "Object" &&typeofp = = = "Object" && o &&p) {a=o[name];b=P[name];if(A = = b) {return typeofminor=== ' function '? minor (O,p): 0;}if(typeofA = = =typeofb) {returnA < b? -1:1;}return typeofA <typeofB? -1:1;}Else{Throw("Error"); }}}varemployees=[]employees[0]={name: "George", age:32, Retiredate: "March 12, 2014"}employees[1]={name: "Edward", Age:17, Retiredate: "June 2, 2023"}employees[2]={name: "Christine", age:58, Retiredate: "December 20, 2036"}employees[3]={name: "Sarah", age:62, Retiredate: "April 30, 2020"}employees.sort (by (' Age ', by (' name ')) ; alert (employees);//Self Test//Array Object Orderingfunctionjsonobjsort (o, p) {varA, B; if(typeofo = = = "Object" &&typeofp = = = "Object" && o &&p) {a=O.serialnum; b=P.serialnum; if(A = = =b) {return0; }        if(typeofA = = =typeofb) {returnA < b? -1:1; }        return typeofA <typeofB? -1:1; } Else {        Throw("Error"); }}varJS;varJsonobj =eval (jsonstring); Jsonobj.sort (jsonobjsort);

Sort JavaScript Array objects

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.