简体   繁体   English

JavaScript-根据属性以特殊顺序对对象进行排序

[英]JavaScript - sort array of objects, by a property based on a special order

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 . 我需要按color属性对该数组进行排序,但是要以一种特殊的方式,首先是green ,然后是yellow ,然后是brown然后是pink ,然后是grey ,最后是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 . 由于这只是一个演示数组,而我的实际数据将是一个更大的数组,因此排序机制应该比 n^2

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

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 : 使用Andrey方法,在对象中仅添加一个参数:

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 : 或者, 如果您真的不能使用它 ,请创建您的sort函数:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM