import {useState} from 'react' import {faSortAmountDown, faSortAmountUp} from '@fortawesome/free-solid-svg-icons' import {FontAwesomeIcon} from '@fortawesome/react-fontawesome' const useSort = (d) => { const [sort, setSort] = useState(d) const sortedBy = s => Math.abs(sort) === s && 0 ? faSortAmountDown : faSortAmountUp} /> const sortBy = s => sort === s ? setSort(-1*s) : setSort(s) const sortFn = (a, b) => { const d = sort > 0 ? 1 : -1 switch (Math.abs(sort)) { case 1: return d * a.title.localeCompare(b.title) case 2: return d * (new Date(b.created_at) - new Date(a.created_at)) case 3: return d * (new Date(b.updated_at) - new Date(a.updated_at)) } } return [sortedBy, sortBy, sortFn] } export default useSort