You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
23 lines
550 B
23 lines
550 B
import { isDate, isNumber } from "./is";
|
|
|
|
export function sorter(a: Recordable<any>, b: Recordable<any>, field: string) : number {
|
|
if (!a[field] && !b[field]) {
|
|
return 0;
|
|
}
|
|
if (a[field] && !b[field]) {
|
|
return 1;
|
|
}
|
|
if (b[field] && !a[field]) {
|
|
return -1;
|
|
}
|
|
if (isDate(a[field])) {
|
|
return a[field] - b[field];
|
|
}
|
|
if (isNumber(a[field])) {
|
|
return a[field] - b[field];
|
|
}
|
|
if (Array.isArray(a[field])) {
|
|
return a[field].length - b[field].length;
|
|
}
|
|
return String(a[field]).localeCompare(String(b[field]));
|
|
}
|
|
|