2

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.

2
  • what about grey? Commented Sep 15, 2016 at 8:51
  • Forgot about grey, i'll edit my question. Grey should be before blonde. Commented Sep 15, 2016 at 8:52

4 Answers 4

9

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];
});
Sign up to request clarification or add additional context in comments.

3 Comments

Yes ! You can just add param :)
Simple and easy. Fantastic.
This was so helpful thank you so much!
3

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);
});

Comments

1

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);

Comments

1

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));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.