diff --git a/frontend/beCompliant/src/components/Table.tsx b/frontend/beCompliant/src/components/Table.tsx index 707123fa2..d4cbf3f60 100644 --- a/frontend/beCompliant/src/components/Table.tsx +++ b/frontend/beCompliant/src/components/Table.tsx @@ -17,6 +17,7 @@ import { DataTableCell } from './table/DataTableCell'; import { DataTableHeader } from './table/DataTableHeader'; import { TableCell } from './table/TableCell'; import { OptionalField, Question, Table } from '../api/types'; +import { getSortFuncForColumn } from './table/TableSort'; type Props = { data: Question[]; @@ -72,18 +73,14 @@ export function TableComponent({ data, tableData }: Props) { ( a.getValue(columnId) as OptionalField | null )?.value?.[0]?.toLowerCase() || ''; + const valueB = ( b.getValue(columnId) as OptionalField | null )?.value?.[0]?.toLowerCase() || ''; - if (valueA < valueB) { - return -1; - } - if (valueA > valueB) { - return 1; - } - return 0; + const sortFunc = getSortFuncForColumn(columnId); + return sortFunc(valueA, valueB); }, }) ); diff --git a/frontend/beCompliant/src/components/table/TableSort.ts b/frontend/beCompliant/src/components/table/TableSort.ts new file mode 100644 index 000000000..02ef42f24 --- /dev/null +++ b/frontend/beCompliant/src/components/table/TableSort.ts @@ -0,0 +1,42 @@ +type SortFunc = (a: string, b: string) => number; + +export const standardColumnSort: SortFunc = (a, b) => { + return a.localeCompare(b); +}; + +const customSort = (a: string, b: string, priorityOrder: string[]): number => { + const priValueA = priorityOrder.indexOf(a); + const priValueB = priorityOrder.indexOf(b); + + // If for some reason both values are not in the priority order list, lets use standard-sort + if (priValueA < 0 && priValueB < 0) { + return standardColumnSort(a, b); + } + + return priValueA - priValueB; +}; + +export const prirorityColumnSort: SortFunc = (a, b) => { + const priorityOrder = ['kan', 'bør', '(må)', 'må']; + return customSort(a, b, priorityOrder); +}; + +export const ledetidColumnSort: SortFunc = (a, b) => { + const priorityOrder = [ + 'kort (< 1 mnd)', + 'middels (1-3 mnd)', + 'lang (> 3 mnd)', + ]; + return customSort(a, b, priorityOrder); +}; + +export const getSortFuncForColumn = (columnId: string): SortFunc => { + switch (columnId.toLowerCase()) { + case 'pri': + return prirorityColumnSort; + case 'ledetid': + return ledetidColumnSort; + default: + return standardColumnSort; + } +};