admin管理员组

文章数量:1313616

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 pactor 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.

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 pactor 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.

Share Improve this question edited May 23, 2017 at 11:51 CommunityBot 11 silver badge asked Sep 15, 2016 at 8:47 AlexAlex 2,0724 gold badges42 silver badges76 bronze badges 2
  • what about grey? – Nina Scholz Commented Sep 15, 2016 at 8:51
  • Forgot about grey, i'll edit my question. Grey should be before blonde. – Alex Commented Sep 15, 2016 at 8:52
Add a ment  | 

4 Answers 4

Reset to default 9

Here is your parator

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

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

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

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

本文标签: javascriptSort array of objectsby a property based on a special orderStack Overflow