This is the proposed solution:
public sortByPredicate(dataList: any[], predicate: string): any[] {
return dataList.sort((a: string, b: string) => {
if ((a[predicate] > b[predicate])) {
return 1;
} else {
if (b[predicate] > a[predicate]) {
return -1;
} else {
return 0;
}
}
});
}
Some else
statements are unnecessary; this presents a more concise form.
public sortByPredicate3(dataList: any[], predicate: string): any[] {
return dataList.sort((a: string, b: string) => {
if ((a[predicate] > b[predicate])) {
return 1;
}
if (b[predicate] > a[predicate]) {
return -1;
}
return 0;
});
}