I have an array of objects like this. I am trying to sort the array if the holidays have the same holiday_on, and Sort by holiday_for By Descending Order.
const holidays: IHoliday[] = [
{
id: 1,
holiday_for: 1,
holiday_on: "2021-10-26"
},
{
id: 2,
holiday_for: 3,
holiday_on: "2021-10-26"
},
{
id: 3,
holiday_for: 1,
holiday_on: "2021-11-26"
},
{
id: 4,
holiday_for: 3,
holiday_on: "2021-11-26"
},
{
id: 5,
holiday_for: 4,
holiday_on: "2021-11-26"
}
]
With the implementation below getting this type error Argument of type '(a: IHoliday, b: IHoliday) => number | undefined' is not assignable to parameter of type '(a: IHoliday, b: IHoliday) => number'.Type 'number | undefined' is not assignable to type 'number'
If I make the holidays array any type then typescript giving another warning - Not all code paths return a value.
Implementation
const sorted = holidays.sort((a: IHoliday, b: IHoliday) => {
if (a.holiday_on === b.holiday_on) {
return b.holiday_for - a.holiday_for;
}
});