From 15911a67c03f2ae6298b2de4ace8cef84b03692a Mon Sep 17 00:00:00 2001 From: kaulfield23 Date: Mon, 4 Sep 2023 17:18:26 +0200 Subject: [PATCH 1/3] add sortComparator to person tag view cell --- .../columnTypes/PersonTagColumnType.tsx | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/features/views/components/ViewDataTable/columnTypes/PersonTagColumnType.tsx b/src/features/views/components/ViewDataTable/columnTypes/PersonTagColumnType.tsx index 59f7c6d701..8e7a9e7307 100644 --- a/src/features/views/components/ViewDataTable/columnTypes/PersonTagColumnType.tsx +++ b/src/features/views/components/ViewDataTable/columnTypes/PersonTagColumnType.tsx @@ -41,6 +41,24 @@ export default class PersonTagColumnType implements IColumnType { /> ); }, + sortComparator: (v1: ZetkinTag, v2: ZetkinTag) => { + if (v1 == null || v2 == null) { + return 0; + } + + if (typeof v1.value === 'string' && typeof v2.value === 'string') { + if (Number.isNaN(parseInt(v1.value))) { + return v1.value.localeCompare(v2.value); + } else { + return parseInt(v1.value) - parseInt(v2.value); + } + } + + if (typeof v1.value === 'number' && typeof v2.value === 'number') { + return v1.value - v2.value; + } + return 0; + }, }; } From dcbece40c0b62a9ceeb69b4ee789beff40b45581 Mon Sep 17 00:00:00 2001 From: kaulfield23 Date: Tue, 5 Sep 2023 16:29:07 +0200 Subject: [PATCH 2/3] sort when value is null --- .../columnTypes/PersonTagColumnType.tsx | 23 +++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/src/features/views/components/ViewDataTable/columnTypes/PersonTagColumnType.tsx b/src/features/views/components/ViewDataTable/columnTypes/PersonTagColumnType.tsx index 8e7a9e7307..8cf96fcff6 100644 --- a/src/features/views/components/ViewDataTable/columnTypes/PersonTagColumnType.tsx +++ b/src/features/views/components/ViewDataTable/columnTypes/PersonTagColumnType.tsx @@ -42,16 +42,31 @@ export default class PersonTagColumnType implements IColumnType { ); }, sortComparator: (v1: ZetkinTag, v2: ZetkinTag) => { - if (v1 == null || v2 == null) { - return 0; + // 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 (Number.isNaN(parseInt(v1.value))) { + if (isNaN(Number(v1.value)) && isNaN(Number(v2.value))) { return v1.value.localeCompare(v2.value); - } else { + } + 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 { + v1.value.localeCompare(v2.value); + } } if (typeof v1.value === 'number' && typeof v2.value === 'number') { From 5e188c4de472b682647d8c0089f53d70f2ffe257 Mon Sep 17 00:00:00 2001 From: kaulfield23 Date: Thu, 7 Sep 2023 12:02:09 +0200 Subject: [PATCH 3/3] add return --- .../ViewDataTable/columnTypes/PersonTagColumnType.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/features/views/components/ViewDataTable/columnTypes/PersonTagColumnType.tsx b/src/features/views/components/ViewDataTable/columnTypes/PersonTagColumnType.tsx index 8cf96fcff6..312ec245ce 100644 --- a/src/features/views/components/ViewDataTable/columnTypes/PersonTagColumnType.tsx +++ b/src/features/views/components/ViewDataTable/columnTypes/PersonTagColumnType.tsx @@ -65,7 +65,7 @@ export default class PersonTagColumnType implements IColumnType { if (isNaN(Number(v2.value))) { return -1; } else { - v1.value.localeCompare(v2.value); + return v1.value.localeCompare(v2.value); } }