import { get } from 'lodash';

export class ObjectSorter {
  private static defaultCollator: Intl.Collator = new Intl.Collator('en', {
    numeric: true,
    caseFirst: 'upper',
  });

  static sort<T>(
    objects: Array<T>,
    sortProp: string,
    direction: number = 1,
    collator: Intl.Collator = null
  ): Array<T> {
    if (!collator) collator = this.defaultCollator;

    objects.sort((a, b) => {
      let aVal = get(a, sortProp);
      let bVal = get(b, sortProp);
      return collator.compare(aVal, bVal) * direction;
    });

    return objects;
  }

  static getCollator(): Intl.Collator {
    return this.defaultCollator;
  }
}
