export function getPredicateFn()

in packages/charts/src/common/predicate.ts [24:53]


export function getPredicateFn<T>(predicate: Predicate, locale: string, accessor?: keyof T): (a: T, b: T) => number {
  switch (predicate) {
    case 'alphaAsc':
      return (a: T, b: T) => {
        const aValue = String(accessor ? a[accessor] : a);
        const bValue = String(accessor ? b[accessor] : b);
        return aValue.localeCompare(bValue, locale);
      };
    case 'alphaDesc':
      return (a: T, b: T) => {
        const aValue = String(accessor ? a[accessor] : a);
        const bValue = String(accessor ? b[accessor] : b);
        return bValue.localeCompare(aValue, locale);
      };
    case 'numDesc':
      return (a: T, b: T) => {
        const aValue = Number(accessor ? a[accessor] : a);
        const bValue = Number(accessor ? b[accessor] : b);
        return bValue - aValue;
      };
    case 'numAsc':
      return (a: T, b: T) => {
        const aValue = Number(accessor ? a[accessor] : a);
        const bValue = Number(accessor ? b[accessor] : b);
        return aValue - bValue;
      };
    case 'dataIndex':
      return () => 0;
  }
}