diff --git a/src/features/views/components/ViewDataTable/columnTypes/PersonTagColumnType.tsx b/src/features/views/components/ViewDataTable/columnTypes/PersonTagColumnType.tsx index 59f7c6d701..312ec245ce 100644 --- a/src/features/views/components/ViewDataTable/columnTypes/PersonTagColumnType.tsx +++ b/src/features/views/components/ViewDataTable/columnTypes/PersonTagColumnType.tsx @@ -41,6 +41,39 @@ export default class PersonTagColumnType implements IColumnType { /> ); }, + sortComparator: (v1: ZetkinTag, v2: ZetkinTag) => { + // TODO: simplify code when v1.value, v2.value can't be null + //if v1 or v1.value is null and v2 or v2.value is null then it should return 0 otherwise v1(null) should be placed before v2(value) + if (!v1 || !v1.value) { + return !v2 || !v2.value ? 0 : -1; + } + //if v2 or v2.value is null then v1(value) should be placed after v2(null) + if (!v2 || !v2.value) { + return 1; + } + + if (typeof v1.value === 'string' && typeof v2.value === 'string') { + if (isNaN(Number(v1.value)) && isNaN(Number(v2.value))) { + return v1.value.localeCompare(v2.value); + } + if (!isNaN(Number(v1.value)) && !isNaN(Number(v2.value))) { + return parseInt(v1.value) - parseInt(v2.value); + } + if (isNaN(Number(v1.value))) { + return 1; + } + if (isNaN(Number(v2.value))) { + return -1; + } else { + return v1.value.localeCompare(v2.value); + } + } + + if (typeof v1.value === 'number' && typeof v2.value === 'number') { + return v1.value - v2.value; + } + return 0; + }, }; }