In JavaScript, implement multi-dimensional arrays and object array sorting, basically using the native sort() method to sort the elements of the array.
I won’t talk about its basic usage, let’s first look at a simple sorting example:
//Sortalphabetically and ascending:varmyarray=["Bob","Bully","Amy"]myarray.sort()//Arraynow becomes ["Amy", "Bob", "Bully"]
After the array is called sort() directly, the array sorts the elements in the array alphabetically. To put it more accurately, it sorts in the order of character encoding.
Let’s take a look at the sorting of numbers:
//Sortnumericly and ascending:varmyarray=[25, 8, 7, 41]myarray.sort(function(a,b){returna - b}) //Arraynow becomes [7, 8, 25, 41]sort(fun) accepts a sorting function, which compares the sizes of 2 numbers. The principle of our object array sorting is actually the same.
For object array sorting, we first write a function that constructs the comparison function:
//by function takes a member name string as a parameter // and returns a comparison function that can be used to sort an array of objects containing the member varby = function(name){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) {return < b ? -1 : 1;}returntypeof a < typeofb ? -1 : 1;}else{throw("error");}}}}Array to sort:
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"}Call the function directly:
employees.sort(by("age"));At this point, the ordering of object arrays is basically implemented. So how to implement multiple key-value sorting? It means first sorting the age, and if the age is the same, then comparing the name.
At this time, we can further modify the by function so that it can accept the second parameter. When the main key value produces a match, another compare method will be called to determine the level.
//by function accepts a member name string and an optional secondary comparison function as parameters // and returns a comparison function that can be used to sort the object array that contains the member// When o[age] and p[age] are equal, the secondary comparison function is used to determine the level of 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')));OK, now you can use it with confidence. If you don't understand it, you can copy the by function directly into your application and call it directly.
The above is the full description of the JavaScript object array sorting example method introduced by the editor to you. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support to Wulin.com website!