Reputation: 167
sorry for long subject, I just don't understand the response.
my code:
this.rezerwacjeFilteredByseaarchInput.sort(function (a, b) {
if (a[5]===null) {
// console.log(a[5]);
return 1;
}
if (firmaSortOrder) {
return a[5] > b[5];
}
else {
return b[5] > a[5];
}
});
js throws: error TS2345: Argument of type '(a: Rezerwacja, b: Rezerwacja) => boolean | 1' is not assignable to parameter of type '(a: Rezerwacja, b: Rezerwacja) => number'. Type 'boolean | 1' is not assignable to type 'number'. Type 'true' is not assignable to type 'number'.
Upvotes: 0
Views: 924
Reputation: 2032
This is a typescript compilation error. the signature in rezerwacjeFilteredByseaarchInput is incorrect. It should only return number values (this is what the message is saying ) but in its body you can read that it can return the boolean values.
Upvotes: 0
Reputation: 2682
According to MDN description of sort function, sort compare function has to return a number. Your first condition returns number, but other two returns boolean values. Below code should work.
this.rezerwacjeFilteredByseaarchInput.sort(function (a, b) {
if (a[5] === null) {
return 1;
}
if (firmaSortOrder) {
return a[5] - b[5];
}
return b[5] - a[5];
});
Upvotes: 1