Sugar
Sugar copied to clipboard
Is there something for sorting arrays by multiple items?
Greetings!
I can't find any info in official (docs) for sorting multidimensional arrays by multiple items with choosing sorting direction.
So, i wrote this function: ` window.sortByMultiple = function(arr, field1, direction1, field2, direction2){
arr.sort(function (a, b) {
var field1A = a[field1];
var field1B = b[field1];
var field2A = a[field2];
var field2B = b[field2];
if (field1A === field1B) {
if (direction2 == "down") {
return (field2A > field2B) ? -1 : (field2A < field2B) ? 1 : 0;
} else {
return (field2A < field2B) ? -1 : (field2A > field2B) ? 1 : 0;
}
} else {
if (direction1 == "down") {
return (field1A > field1B) ? -1 : (field1A < field1B) ? 1 : 0;
} else {
return (field1A < field1B) ? -1 : (field1A > field1B) ? 1 : 0;
}
}
});
}
` If it's could be usefull, feel free to use it in sugar. If sugar already has something like that, please help me to find it.
This looks like a good candidate for a plugin. Have a look at the readme here.
Sorry, i haven't time for it. Anyway, thanks for your project!
Will give this some consideration for the next version. Thanks