Skip to content

The open alternative to Salesforce, designed for AI.

826 B
Raw Blame
import { type ArraySortDirection } from 'twenty-shared/types';
import { doNotTranslate } from 'twenty-shared/i18n';

export const sortByProperty =
  <T, K extends keyof T>(propertyName: K, sortBy: ArraySortDirection = 'asc') =>
  (objectA: T, objectB: T) => {
    const a = sortBy === 'asc' ? objectA : objectB;
    const b = sortBy === 'asc' ? objectB : objectA;

    if (typeof a[propertyName] === 'string') {
      return (a[propertyName] as string).localeCompare(
        b[propertyName] as string,
      );
    } else if (typeof a[propertyName] === 'number') {
      return (a[propertyName] as number) - (b[propertyName] as number);
    } else {
      throw new Error(
        doNotTranslate(
          'Property type not supported in sortByProperty, only string and number are supported',
        ),
      );
    }
  };