diff --git a/webui/src/components/widgets/Table.svelte b/webui/src/components/widgets/Table.svelte index b92301d..e490cf1 100644 --- a/webui/src/components/widgets/Table.svelte +++ b/webui/src/components/widgets/Table.svelte @@ -113,7 +113,8 @@ entries.forEach((entry) => { addSortKeys( entry.entity, - $labelListing.getObject(entry.entity).identify() + $labelListing.getObject(entry.entity).identify(), + false ); if (entry.value.t === "Address") { diff --git a/webui/src/util/sort.ts b/webui/src/util/sort.ts index f5bb89b..3ab5a22 100644 --- a/webui/src/util/sort.ts +++ b/webui/src/util/sort.ts @@ -3,44 +3,43 @@ import type { UpEntry } from "upend"; export type SortKeys = { [key: string]: string[] }; export function sortByValue(entries: UpEntry[], sortKeys: SortKeys): void { - entries - .sort((aEntry, bEntry) => { - return String(aEntry.value.c).length - String(bEntry.value.c).length; - }) - .sort((aEntry, bEntry) => { - if (aEntry.value.t === "Number" && bEntry.value.t === "Number") { - return bEntry.value.c - aEntry.value.c; - } + entries.sort((aEntry, bEntry) => { + return String(aEntry.value.c).length - String(bEntry.value.c).length; + }); + entries.sort((aEntry, bEntry) => { + if (aEntry.value.t === "Number" && bEntry.value.t === "Number") { + return bEntry.value.c - aEntry.value.c; + } + if ( + !sortKeys[aEntry.value.c]?.length || + !sortKeys[bEntry.value.c]?.length + ) { if ( - !sortKeys[aEntry.value.c]?.length || + Boolean(sortKeys[aEntry.value.c]?.length) && !sortKeys[bEntry.value.c]?.length ) { - if ( - Boolean(sortKeys[aEntry.value.c]?.length) && - !sortKeys[bEntry.value.c]?.length - ) { - return -1; - } else if ( - !sortKeys[aEntry.value.c]?.length && - Boolean(sortKeys[bEntry.value.c]?.length) - ) { - return 1; - } else { - return String(aEntry.value.c).localeCompare( - String(bEntry.value.c), - undefined, - { numeric: true, sensitivity: "base" } - ); - } + return -1; + } else if ( + !sortKeys[aEntry.value.c]?.length && + Boolean(sortKeys[bEntry.value.c]?.length) + ) { + return 1; } else { - return sortKeys[aEntry.value.c][0].localeCompare( - sortKeys[bEntry.value.c][0], + return String(aEntry.value.c).localeCompare( + String(bEntry.value.c), undefined, { numeric: true, sensitivity: "base" } ); } - }); + } else { + return sortKeys[aEntry.value.c][0].localeCompare( + sortKeys[bEntry.value.c][0], + undefined, + { numeric: true, sensitivity: "base" } + ); + } + }); } export function sortByAttribute(entries: UpEntry[]): void { @@ -50,34 +49,30 @@ export function sortByAttribute(entries: UpEntry[]): void { } export function sortByEntity(entries: UpEntry[], sortKeys: SortKeys): void { - entries - .sort((aEntry, bEntry) => { - return aEntry.attribute.localeCompare(bEntry.attribute); - }) - .sort((aEntry, bEntry) => { + entries.sort((aEntry, bEntry) => { + return aEntry.attribute.localeCompare(bEntry.attribute); + }); + entries.sort((aEntry, bEntry) => { + if (!sortKeys[aEntry.entity]?.length || !sortKeys[bEntry.entity]?.length) { if ( - !sortKeys[aEntry.entity]?.length || + Boolean(sortKeys[aEntry.entity]?.length) && !sortKeys[bEntry.entity]?.length ) { - if ( - Boolean(sortKeys[aEntry.entity]?.length) && - !sortKeys[bEntry.entity]?.length - ) { - return -1; - } else if ( - !sortKeys[aEntry.entity]?.length && - Boolean(sortKeys[bEntry.entity]?.length) - ) { - return 1; - } else { - return aEntry.entity.localeCompare(bEntry.entity); - } + return -1; + } else if ( + !sortKeys[aEntry.entity]?.length && + Boolean(sortKeys[bEntry.entity]?.length) + ) { + return 1; } else { - return sortKeys[aEntry.entity][0].localeCompare( - sortKeys[bEntry.entity][0] - ); + return aEntry.entity.localeCompare(bEntry.entity); } - }); + } else { + return sortKeys[aEntry.entity][0].localeCompare( + sortKeys[bEntry.entity][0] + ); + } + }); } export function defaultEntitySort( @@ -85,9 +80,9 @@ export function defaultEntitySort( sortKeys: SortKeys ): UpEntry[] { const result = entries.concat(); - sortByValue(entries, sortKeys); - sortByAttribute(entries); - sortByEntity(entries, sortKeys); + sortByValue(result, sortKeys); + sortByAttribute(result); + sortByEntity(result, sortKeys); return result; } @@ -96,8 +91,8 @@ export function entityValueSort( sortKeys: SortKeys ): UpEntry[] { const result = entries.concat(); - sortByEntity(entries, sortKeys); - sortByAttribute(entries); - sortByValue(entries, sortKeys); + sortByEntity(result, sortKeys); + sortByAttribute(result); + sortByValue(result, sortKeys); return result; }