在数组中,如何使用.sort()将基于一个属性的对象组合在一起?

时间:2017-03-21 17:01:34

标签: javascript arrays sorting

使用Javascripts .sort(),我有一个具有'类别'的对象列表。财产和子类别'属性。到目前为止,我使用item1.category().localeCompare(item2.category())按类别排序列表,但在每个类别组中,我希望按子类别对对象进行排序。

到目前为止,我的阵列分类为:

[
  {'category':'cat1', 'subcategory':'subcat1'},
  {'category':'cat1', 'subcategory':'subcat2'},
  {'category':'cat1', 'subcategory':'subcat1'},
  {'category':'cat1', 'subcategory':'subcat2'},
  {'category':'cat1', 'subcategory':'subcat2'},
  {'category':'cat1', 'subcategory':'subcat1'},
  {'category':'cat1', 'subcategory':'subcat2'},
  {'category':'cat2', 'subcategory':'subcat1'},
  {'category':'cat2', 'subcategory':'subcat2'},
  {'category':'cat2', 'subcategory':'subcat1'},
  {'category':'cat2', 'subcategory':'subcat2'},
  {'category':'cat2', 'subcategory':'subcat2'},
  {'category':'cat2', 'subcategory':'subcat1'},
  {'category':'cat2', 'subcategory':'subcat2'}
]

我需要它像这样:

[
  {'category':'cat1', 'subcategory':'subcat1'},
  {'category':'cat1', 'subcategory':'subcat1'},
  {'category':'cat1', 'subcategory':'subcat1'},
  {'category':'cat1', 'subcategory':'subcat2'},
  {'category':'cat1', 'subcategory':'subcat2'},
  {'category':'cat1', 'subcategory':'subcat2'},
  {'category':'cat1', 'subcategory':'subcat2'},
  {'category':'cat2', 'subcategory':'subcat1'},
  {'category':'cat2', 'subcategory':'subcat1'},
  {'category':'cat2', 'subcategory':'subcat1'},
  {'category':'cat2', 'subcategory':'subcat2'},
  {'category':'cat2', 'subcategory':'subcat2'},
  {'category':'cat2', 'subcategory':'subcat2'},
  {'category':'cat2', 'subcategory':'subcat2'}
]

此外,我只能使用一种()并且不能做任何事情,因为它作为参数传递给另一个函数。

5 个答案:

答案 0 :(得分:3)

只需在sort函数中添加一个额外的谓词:

const arr = [ 
  { "field1": "a2", "field2": "b2" },  
  { "field1": "a1", "field2": "b2" },
  { "field1": "a1", "field2": "b1" }
];

arr.sort((a,b) => {
  return a.field1.localeCompare(b.field1) || a.field2.localeCompare(b.field2);
});
// [ {"field1":"a1","field2":"b1"},
//   {"field1":"a1","field2":"b2"},
//   {"field1":"a2","field2":"b2"}]

答案 1 :(得分:1)

您可以在子类别上添加另一个localeCompare,例如:

arr.sort(function (a,b) {
    return a.category.localeCompare(b.category)
        || a.subcategory.localeCompare(b.subcategory);
})

答案 2 :(得分:0)

只需扩展您的排序功能。

 categories.sort(function(a, b) {
   if(a.category < b.category){
     return -1;  
   }else if(a.category > b.category){
     return 1;
   }else{
     if(a.subcategory < b.subcategory){
       return -1
     }else if(a.subcategory > b.subcategory){
       return 1;
     }
   }
 });

请看这里的工作小提琴:http://jsfiddle.net/Sergey_Mell/htv6zs8g/

答案 3 :(得分:0)

试试这个:

var values = [
  {'category':'cat1', 'subcategory':'subcat1'},
  {'category':'cat1', 'subcategory':'subcat2'},
  {'category':'cat1', 'subcategory':'subcat1'},
  {'category':'cat1', 'subcategory':'subcat2'},
  {'category':'cat1', 'subcategory':'subcat2'},
  {'category':'cat1', 'subcategory':'subcat1'},
  {'category':'cat1', 'subcategory':'subcat2'},
  {'category':'cat2', 'subcategory':'subcat1'},
  {'category':'cat2', 'subcategory':'subcat2'},
  {'category':'cat2', 'subcategory':'subcat1'},
  {'category':'cat2', 'subcategory':'subcat2'},
  {'category':'cat2', 'subcategory':'subcat2'},
  {'category':'cat2', 'subcategory':'subcat1'},
  {'category':'cat2', 'subcategory':'subcat2'}
];
console.log(values.sort(function (a, b) {
  var tmp_a = a.category + a.subcategory;
  var tmp_b = b.category + b.subcategory;

  if (tmp_a < tmp_b) {
    return -1;
  }
  if (tmp_a > tmp_b) {
    return 1;
  }
  // a must be equal to b
  return 0;
}));

答案 4 :(得分:0)

这对我有用:

arr.sort(function(a, b) {
    if (a.category != b.category) {
        return (a.category > b.category) ? 1 : -1;
    } else {
        return (a.subcategory > b.subcategory) ? 1 : -1;
    }
    return 0;
});

检查 category 是否不同。如果它不同,那么就按它排序。否则(如果类别是相同的),它按子类别排序。