I have the following code:
this.userActionsRights$ = combineLatest([this.sortBy$, this.userActionsRights$]).pipe(
map(([sortBy, userRights]) => {
const { active, direction } = sortBy;
const sortByAsc = direction === 'asc';
let sortFn;
if (active === 'name') sortFn = compareString;
if (active === 'id') sortFn = compareNumber;
return userRights.sort(sortFn);
}),
);
Where compare string is:
export function compareString(a: string | undefined, b: string | undefined, isAsc: boolean) {
if (a === undefined || a === null) return isAsc ? 1 : -1
if (b === undefined || b === null) return isAsc ? -1 : 1
return isAsc ? b.localeCompare(a) : a.localeCompare(b)
}
How to pass sortByAsc
to compareString
and then apply it below in userRights.sort(sortFn);
.