mirror of
https://github.com/kmc7468/arkvault.git
synced 2025-12-16 06:58:46 +00:00
카테고리 목록 이름 기반 정렬 구현
This commit is contained in:
@@ -27,3 +27,32 @@ export const formatNetworkSpeed = (speed: number) => {
|
||||
if (speed < 1000 * 1000 * 1000) return `${(speed / 1000 / 1000).toFixed(1)} Mbps`;
|
||||
return `${(speed / 1000 / 1000 / 1000).toFixed(1)} Gbps`;
|
||||
};
|
||||
|
||||
export enum SortBy {
|
||||
NAME_ASC,
|
||||
NAME_DESC,
|
||||
}
|
||||
|
||||
type SortFunc = (a?: string, b?: string) => number;
|
||||
|
||||
const collator = new Intl.Collator(undefined, { numeric: true, sensitivity: "base" });
|
||||
|
||||
const sortByNameAsc: SortFunc = (a, b) => {
|
||||
if (a && b) return collator.compare(a, b);
|
||||
if (a) return -1;
|
||||
if (b) return 1;
|
||||
return 0;
|
||||
};
|
||||
|
||||
const sortByNameDesc: SortFunc = (a, b) => -sortByNameAsc(a, b);
|
||||
|
||||
export const sortEntries = <T extends { name?: string }>(entries: T[], sortBy: SortBy) => {
|
||||
let sortFunc: SortFunc;
|
||||
if (sortBy === SortBy.NAME_ASC) {
|
||||
sortFunc = sortByNameAsc;
|
||||
} else {
|
||||
sortFunc = sortByNameDesc;
|
||||
}
|
||||
|
||||
entries.sort((a, b) => sortFunc(a.name, b.name));
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user