在数组中,如何使用.sort()对按另一个属性排序的对象进行分组?

时间:2022-02-01 13:18:57

Using Javascripts .sort(), I have a list of objects that have a 'category' property and a 'subcategory' property. So far I have the list sorted by category using item1.category().localeCompare(item2.category()) but within each category group I want to have the objects sorted by subcategory.

使用javascript .sort(),我有一个对象列表,这些对象具有“类别”属性和“子类别”属性。到目前为止,我已经使用item1.category(). localecompare (item2.category())对列表进行了排序,但是在每个category组中,我想让对象按子类进行排序。

So far my array sorts into something like this:

到目前为止,我的数组排序如下

[
  {'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'}
]

Whereas I need it to be like this:

而我需要这样:

[
  {'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'}
]

Also I can only use one sort() and can't do anything outside of it as it's being passed as a param to another function.

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

5 个解决方案

#1


3  

Just add an additional predicate to your sort function:

只需在排序函数中添加一个附加谓词:

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"}]

#2


1  

You could add another localeCompare on the subcategory, eg:

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

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

#3


0  

Just expand your sorting function.

展开排序函数。

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

Look at the working fiddle here: http://jsfiddle.net/Sergey_Mell/htv6zs8g/

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

#4


0  

try this:

试试这个:

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

#5


0  

This works for me:

这工作对我来说:

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

It checks to see if category is different. If it is different, then it sorts by that. Else (if category is the same), it sorts by subcategory.

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

#1


3  

Just add an additional predicate to your sort function:

只需在排序函数中添加一个附加谓词:

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"}]

#2


1  

You could add another localeCompare on the subcategory, eg:

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

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

#3


0  

Just expand your sorting function.

展开排序函数。

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

Look at the working fiddle here: http://jsfiddle.net/Sergey_Mell/htv6zs8g/

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

#4


0  

try this:

试试这个:

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

#5


0  

This works for me:

这工作对我来说:

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

It checks to see if category is different. If it is different, then it sorts by that. Else (if category is the same), it sorts by subcategory.

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