You need to take undefined into consideration. I think it's a waste of resources to sort an array multiple times, because there areof the numerous loops going onthat the sortings will demand.
const currentPets = [
{ state: 'empty', pet: null},
{ state: 'empty', pet: null},
{ state: 'empty', pet: null},
{ state: 'empty', pet: null},
{ state: 'empty', pet: null},
{ state: 'occupied', pet: { type: 'quartz', level: 2, isFavorite: false}},
{ state: 'occupied', pet: { type: 'quartz', level: 3, isFavorite: false}},
{ state: 'equipped', pet: { type: 'quartz', level: 6, isFavorite: true}},
{ state: 'occupied', pet: { type: 'quartz', level: 2, isFavorite: false}},
{ state: 'occupied', pet: { type: 'obsidian', level: 2, isFavorite: false}},
{ state: 'occupied', pet: { type: 'obsidian', level: 5, isFavorite: false}},
{ state: 'occupied', pet: { type: 'obsidian', level: 6, isFavorite: true}},
{ state: 'empty', pet: null},
{ state: 'empty', pet: null},
{ state: 'occupied', pet: { type: 'agate', level: 1, isFavorite: false}},
{ state: 'occupied', pet: { type: 'agate', level: 2, isFavorite: false}},
{ state: 'occupied', pet: { type: 'agate', level: 4, isFavorite: true}},
]
const sortAll = (arr) => {
const UNSORTED = 0;
let tempA, tempB, result = 0;
return arr
.sort((a, b) => {
result = compareProperties(a.pet?.isFavorite, b.pet?.isFavorite)
// if (result == UNSORTED) {
// result = compareProperties(a.pet?.type, b.pet?.type)
// }
// if (result == UNSORTED) {
// ...
return result
})
}
const compareProperties = (tempA, tempB) => {
if (tempA !== tempB) {
if (typeof tempA == 'undefined') { return 1 }
if (typeof tempB == 'undefined') { return -1 }
if (tempA < tempB) { return 1 }
if (tempA > tempB) { return -1 }
}
return 0
}
console.log('sorted', sortAll(currentPets));