Array.prototype allows us to sort array just by 1 parameter. I have created my own method 'sortBy' to fix this gap. The method also allows to sort by desc. Let's say we have the following array
var objects = [{'Name': 'John', 'Age': 23, 'Profession': 'Accountant'},
{'Name': 'Adam', 'Age': 18, 'Profession': 'Engineer'},
{'Name': 'Vik', 'Age': 46, 'Profession': 'Architect'},
{'Name': 'Jane', 'Age': 36, 'Profession': 'Developer'},
{'Name': 'Rikky', 'Age': 28, 'Profession': 'Developer'}];
- var objects = [{'Name': 'John', 'Age': 23, 'Profession': 'Accountant'},
- {'Name': 'Adam', 'Age': 18, 'Profession': 'Engineer'},
- {'Name': 'Vik', 'Age': 46, 'Profession': 'Architect'},
- {'Name': 'Jane', 'Age': 36, 'Profession': 'Developer'},
- {'Name': 'Rikky', 'Age': 28, 'Profession': 'Developer'}];
var objects = [{'Name': 'John', 'Age': 23, 'Profession': 'Accountant'},
{'Name': 'Adam', 'Age': 18, 'Profession': 'Engineer'},
{'Name': 'Vik', 'Age': 46, 'Profession': 'Architect'},
{'Name': 'Jane', 'Age': 36, 'Profession': 'Developer'},
{'Name': 'Rikky', 'Age': 28, 'Profession': 'Developer'}];
The following function call sorts the array by 'Profession' then by 'Name' descendantly
sortBy(objects, 'Profession', {'Name': 'desc'});
console.table(objects);
- sortBy(objects, 'Profession', {'Name': 'desc'});
- console.table(objects);
sortBy(objects, 'Profession', {'Name': 'desc'});
console.table(objects);
The result will be the following:
Now let's have a look at the method implementation.
var sortBy = function(arr, ...sortByArgs){
arr.sort(function(a,b){
var sortResult = 0;
sortByArgs.forEach(function(arg){
if (sortResult != 0) return;
if(Object.values(arg)[0] == 'desc'){
var propName = Object.keys(arg)[0];
if (a[propName] > b[propName]){
sortResult = -1;
return;
}
if (a[propName] < b[propName]) {
sortResult = 1;
return;
}
}
else {
if (a[arg] < b[arg]){
sortResult = -1;
return;
}
if (a[arg] > b[arg]) {
sortResult = 1;
return;
}
}
});
return sortResult;
});
};
- var sortBy = function(arr, ...sortByArgs){
- arr.sort(function(a,b){
- var sortResult = 0;
- sortByArgs.forEach(function(arg){
- if (sortResult != 0) return;
- if(Object.values(arg)[0] == 'desc'){
- var propName = Object.keys(arg)[0];
- if (a[propName] > b[propName]){
- sortResult = -1;
- return;
- }
- if (a[propName] < b[propName]) {
- sortResult = 1;
- return;
- }
- }
- else {
- if (a[arg] < b[arg]){
- sortResult = -1;
- return;
- }
- if (a[arg] > b[arg]) {
- sortResult = 1;
- return;
- }
- }
- });
-
- return sortResult;
- });
- };
var sortBy = function(arr, ...sortByArgs){
arr.sort(function(a,b){
var sortResult = 0;
sortByArgs.forEach(function(arg){
if (sortResult != 0) return;
if(Object.values(arg)[0] == 'desc'){
var propName = Object.keys(arg)[0];
if (a[propName] > b[propName]){
sortResult = -1;
return;
}
if (a[propName] < b[propName]) {
sortResult = 1;
return;
}
}
else {
if (a[arg] < b[arg]){
sortResult = -1;
return;
}
if (a[arg] > b[arg]) {
sortResult = 1;
return;
}
}
});
return sortResult;
});
};
You can try it on JSFiddler:
http://jsfiddle.net/AndrewBuntsev/cwt2gbej/
Don't forget to press F12 to display browser's console
No comments:
Post a Comment