Working on a web game as a hobby. I have an "inventory" which contains pets:
interface Pet {
  type: string;
  level: number;
  isFavorite: boolean;
}
allPetSlots = [
  {slotState: INVENTORY_STATE.EQUIPPED, pet: {...},
  {slotState: INVENTORY_STATE.OCCUPIED, pet: {...},
  {slotState: INVENTORY_STATE.OCCUPIED, pet: {...},
  {slotState: INVENTORY_STATE.EMPTY, pet: null,
  {slotState: INVENTORY_STATE.EMPTY, pet: null,
  {slotState: INVENTORY_STATE.EMPTY, pet: null,
  {...}
]
I currently have a sortAll method that sorts the inventory on these specific criterias:
- FAVORITED PETS SHOW UP FIRST
 - GROUP BY PET TYPE
 - SORT BY LEVEL desc
 - ALL EMPTY SPOTS SHOW UP AFTER
 
This is what I have:
allPetsSlots
        .sort((a) => (a.state === 'occupied' || a.state === 'equipped' ? -1 : 1))
        .sort((a, b) => {
            if (a.pet?.type < b.pet?.type) return -1;
            if (a.pet?.type > b.pet?.type) return 1;
            return 0;
        })
        .sort((a, b) => {
            if (a.pet?.type === b.pet?.type && a.pet?.level > b.pet?.level) return -1;
            if (a.pet?.type === b.pet?.type && a.pet?.level < b.pet?.level) return 1;
            return 0;
        })
        .sort((a, b) => {
            if (a.pet?.isFavorite || b.pet?.isFavorite) return -1;
            return 1;
        })
While this SEEMS to work in chrome:

I'm having some troubles with other browsers. I'm pretty sure my sort function is doing something weird and my brain is extra foggy today.
Would appreciate any help!
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 = () => {
  currentPets
    .sort((a) => (a.state === 'occupied' || a.state === 'equipped' ? -1 : 1))
        .sort((a, b) => {
            if (a.pet?.type < b.pet?.type) return -1;
            if (a.pet?.type > b.pet?.type) return 1;
            return 0;
        })
        .sort((a, b) => {
            if (a.pet?.type === b.pet?.type && a.pet?.level > b.pet?.level) return -1;
            if (a.pet?.type === b.pet?.type && a.pet?.level < b.pet?.level) return 1;
            return 0;
        })
        .sort((a, b) => {
            if (a.pet?.isFavorite || b.pet?.isFavorite) return -1;
            return 0;
        })
}
console.log('sorted', currentPets)
sortreally works! You can't sort an array in multiple steps, all the logic needs to be in one callback.undefinedinto consideration, which will always return "false", even if comparing to a boolean. So if it'sundefinedcompared totrue, the code won't sort at all.undefinedsince it would sort the occupied/equipped slots to the front and the empty slots to the back. Thats why I include the optional chaining in the subsequent sorts