Brief Analysis of JavaScript Object Array Sorting Example Method

  • 2021-06-28 11:02:03
  • OfStack

In javascript, multidimensional arrays and object arrays are basically sorted using the native sort () method, which is used to sort the elements of the array.

Let's not mention its basic usage, let's start with a simple sorting example:


//Sort
alphabetically and ascending:
var
myarray=["Bob",
"Bully",
"Amy"]
myarray.sort()
//Array
now becomes ["Amy", "Bob", "Bully"]

When sort () is called directly from an array, the elements in the array are sorted alphabetically, more precisely, in the order in which the characters are encoded.

Let's see how the numbers are sorted:


//Sort
numerically and ascending:
var
myarray=[25, 8, 7, 41]
myarray.sort(function(a,b){return
a - b}) //Array
now becomes [7, 8, 25, 41]

sort (fun) accepts a collation function that compares the size of two numbers.And our object array sorting actually works the same way.

For object array sorting, we first write a function to construct a comparison function:


//by Function acceptance 1 Member name string as parameter 
// And back 1 A comparison function that can be used to sort an array of objects containing this 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:


var
employees=[]
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"}

Call the function directly:


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

Here, the object array sorting is almost done.How do I sort multiple keys?This means sorting age first, if age is the same, then comparing name.
At this point, we can go one step further to 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 called to determine whether it is higher or lower.


//by Function acceptance 1 Member name string and 1 Optional secondary comparison functions as parameters 
// And back 1 A comparison function that can be used to sort an array of objects containing the member 
// When o[age]
 and  p[age]  When equal, the secondary comparison function is used to determine the height and the bottom 
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')));

Okay, now you can use it with confidence.If you can't understand it, you can call the copy function directly into your application.


Related articles: