export const stringSortingFunction = (a: string, b: string) =>
  a.toLowerCase().localeCompare(b.toLowerCase())

export const booleanSortingFunction = (a: boolean, b: boolean) =>
  Number(b) - Number(a)

export const numberSortingFunction = (a: number, b: number) => a - b

export const dateSortingFunction = (a: Date, b: Date) =>
  a.getTime() - b.getTime()

export const timestampSortingFunction = (a: string, b: string) =>
  dateSortingFunction(new Date(a), new Date(b))

export const maybeSortingFunction = <T>(
  a: T | null | undefined,
  b: T | null | undefined,
  sortingFunction: (a: T, b: T) => number
) => {
  if ((a === null || a === undefined) && (b === null || b === undefined))
    return 0
  if (a === null || a === undefined) return 1
  if (b === null || b === undefined) return -1

  return sortingFunction(a, b)
}
