我按类型排序此类型的数组:是否有可能基于JavaScript中的另一个对象对排序后的数组进行排序?
const bands = [
{ genre: 'Rap', band: 'Migos', albums: 2},
{ genre: 'Pop', band: 'Coldplay', albums: 4, awards: 10},
{ genre: 'Pop', band: 'xxx', albums: 4, awards: 11},
{ genre: 'Pop', band: 'yyyy', albums: 4, awards: 12},
{ genre: 'Rock', band: 'Breaking zzzz', albums: 1}
{ genre: 'Rock', band: 'Breaking Benjamins', albums: 1}
];
有了这个:
function compare(a, b) {
// Use toUpperCase() to ignore character casing
const genreA = a.genre.toUpperCase();
const genreB = b.genre.toUpperCase();
let comparison = 0;
if (genreA > genreB) {
comparison = 1;
} else if (genreA < genreB) {
comparison = -1;
}
return comparison;
}
由于描述here 但按流派分类后,我也想通过专辑的数量排序。可能吗? TIA
的[分组排序一个JS数组]可能的复制(https://stackoverflow.com/questions/16164078/grouped-sorting-on-a- js-array) – Zenoo
标题与这个问题有什么关系? “基于另一个对象进行排序”与“按多个属性排序” – Andreas
[Javascript,如何对多列上的数组进行排序?](https://stackoverflow.com/questions/2784230/javascript-how- do-you-sort-an-array-on-multiple-columns) – Andreas