import { useCallback, useEffect, useState } from "react"; export enum Direction { ASC = 1, DESC = -1 } export function useSort(items: T[], key: string, direction: Direction = Direction.ASC): T[] { const [ sorted, setSorted ] = useState(items); useEffect(() => { const sorted = [ ...items ] sorted.sort((a: any, b: any) => { if (a[key] > b[key]) return direction; if (a[key] < b[key]) return -direction; return 0 }) setSorted(sorted) }, [key, items, direction]) return sorted }