order an array of objectes typed CollegioVerbale { id: CollegioVerbaleID; idRicUdien:…
- User
order an array of objectes typed CollegioVerbale { id: CollegioVerbaleID; idRicUdien: number; tipoMag: string; gradoMag: number; magistrato?: AnagraficaMagistratoCollegio; riserva: boolean; relatore: boolean; operatore: number; comparso: boolean; oggi: number; } put first all the objects with tipoMag === ‘PRE’ then all the objects with relatore true and order them by gradoMag ascendant then all the objects with tipoMag===‘CON’ and order them by gradoMag ascendant and then at the bottom of array all the objects with riserva true and order them by gradoMag ascendant
- Assistant
const orderedArray = array.sort((a, b) => { if (a.tipoMag === 'PRE' && b.tipoMag !== 'PRE') { return -1; } if (a.tipoMag !== 'PRE' && b.tipoMag === 'PRE') { return 1; } if (a.relatore && !b.relatore) { return -1; } if (!a.relatore && b.relatore) { return 1; } if (a.tipoMag === 'CON' && b.tipoMag !== 'CON') { return 1; } if (a.tipoMag !== 'CON' && b.tipoMag === 'CON') { return -1; } if (a.riserva && !b.riserva) { return 1; } if (!a.riserva && b.riserva) { return -1; } if (a.gradoMag < b.gradoMag) { return -1; } if (a.gradoMag > b.gradoMag) { return 1; } return 0; });