Reputation: 2072
I have an array of objects with multiple properties. Given the following array:
var people = [
{name: "allen", age: 33, color:"green"},
{name: "jon", age: 23, color:"blonde"},
{name: "silver", age: 54, color:"yellow"},
{name: "james", age: 52, color:"grey"},
{name: "flint", age: 25, color:"pink"},
{name: "beilly", age: 31, color:"blonde"},
{name: "bwones", age: 47, color:"grey"},
{name: "sas", age: 35, color:"green"},
{name: "jackson", age: 234, color:"yellow"},
{name: "leonsardo", age: 12, color:"brown"},
{name: "dicaeprio", age: 73, color:"pink"},
{name: "sylvfester", age: 35, color:"blonde"},
{name: "alleen2", age: 33, color:"green"},
{name: "jofn2", age: 23, color:"blonde"},
{name: "sdilver2", age: 54, color:"yellow"},
{name: "jamaes2", age: 52, color:"grey"}
];
I need to sort this array by color
property, but in a special manner, first by green
, then by yellow
, then by brown
then by pink
, then grey
and lastly by blonde
. I read here and here, but having hard time to generate a compactor based upon my needs. Since this is just a demo array and my real data will be a much larger arrays, the sorting mechanism should be quicker than n^2
.
Upvotes: 2
Views: 1451
Reputation: 26201
I guess the proper way of doing this job is by a hash and sort but without using sort the following code might as well turn out to be pretty efficient.
var people = [
{name: "allen", age: 33, color:"green"},
{name: "jon", age: 23, color:"blonde"},
{name: "silver", age: 54, color:"yellow"},
{name: "james", age: 52, color:"grey"},
{name: "flint", age: 25, color:"pink"},
{name: "beilly", age: 31, color:"blonde"},
{name: "bwones", age: 47, color:"grey"},
{name: "sas", age: 35, color:"green"},
{name: "jackson", age: 234, color:"yellow"},
{name: "leonsardo", age: 12, color:"brown"},
{name: "dicaeprio", age: 73, color:"pink"},
{name: "sylvfester", age: 35, color:"blonde"},
{name: "alleen2", age: 33, color:"green"},
{name: "jofn2", age: 23, color:"blonde"},
{name: "sdilver2", age: 54, color:"yellow"},
{name: "jamaes2", age: 52, color:"grey"}
],
arrays = [green, yellow, brown, pink, grey, blonde] = [[],[],[],[],[],[]],
result = [];
Object.keys(people).forEach(k => this[people[k].color].push(people[k]));
result = arrays.reduce((p,c) => p.concat(c));
console.log(result);
For an improved performance you might replace the last line with
result = arrays.reduce((p,c) => (Array.prototype.push.apply(p,c),p));
Upvotes: 1
Reputation: 1487
Use Andrey method, add just one param in your object :
var sortOrder = {green: 0, yellow: 1, brown: 2, pink: 3, grey: 4, blonde: 5};
people.sort(function (p1, p2) {
return sortOrder[p1.color] - sortOrder[p2.color];
});
Or if you really can't use that, create your sort function :
var people =
[
{name: "allen", age: 33, color:"green"},
{name: "jon", age: 23, color:"blonde"},
{name: "silver", age: 54, color:"yellow"},
{name: "james", age: 52, color:"grey"},
{name: "flint", age: 25, color:"pink"},
{name: "beilly", age: 31, color:"blonde"},
{name: "bwones", age: 47, color:"grey"},
{name: "sas", age: 35, color:"green"},
{name: "jackson", age: 234, color:"yellow"},
{name: "leonsardo", age: 12, color:"brown"},
{name: "dicaeprio", age: 73, color:"pink"},
{name: "sylvfester", age: 35, color:"blonde"},
{name: "alleen2", age: 33, color:"green"},
{name: "jofn2", age: 23, color:"blonde"},
{name: "sdilver2", age: 54, color:"yellow"},
{name: "jamaes2", age: 52, color:"grey"}
];
var order = ['green','yellow','brown','pink','grey','blonde'];
function mySort(array)
{
var list = [];
function getElem(array,id)
{
for(var i in array) if(array[i].color == id) list.push(array[i])
}
for(var i in order) getElem(array,order[i]);
return list;
}
mySort(people);
Upvotes: 1
Reputation: 386883
I suggest to use a default value as well for sorting, depending where the non listed color should be sorted.
In this case the properties of the sort order object have to start with a value above zero.
colorOrder = { green: 1, yellow: 2, brown: 3, pink: 4, grey: 5, blonde: 6 };
people.sort(function (a, b) {
return (colorOrder[a.color] || 0) - (colorOrder[b.color] || 0);
});
Upvotes: 3
Reputation: 4050
Here is your comparator
var sortOrder = {green: 0, yellow: 1, brown: 2, pink: 3, grey: 4, blonde: 5};
people.sort(function (p1, p2) {
return sortOrder[p1.color] - sortOrder[p2.color];
});
Upvotes: 9