I am sorting this type of array by genre:
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}
];
With this:
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;
}
As describe here But after sorting by genre, I also want to sort it by number of albums.Is it possible? TIA