From 2aa6fa0ee35dfcdeec791320302050879be4e3f9 Mon Sep 17 00:00:00 2001 From: "Yassine R." Date: Tue, 17 Dec 2024 15:00:44 +0100 Subject: [PATCH] fix(frontend): fix sort of ref --- .../search/sorter/dataSorter.service.spec.ts | 10 +++++++-- .../src/search/sorter/dataSorter.service.ts | 22 +++++++------------ .../services/usagersSorter.service.spec.ts | 8 +++---- .../services/usagersSorter.service.ts | 11 ++++++---- 4 files changed, 27 insertions(+), 24 deletions(-) diff --git a/packages/common/src/search/sorter/dataSorter.service.spec.ts b/packages/common/src/search/sorter/dataSorter.service.spec.ts index a3d84ba2cb..4dd7f2ff71 100644 --- a/packages/common/src/search/sorter/dataSorter.service.spec.ts +++ b/packages/common/src/search/sorter/dataSorter.service.spec.ts @@ -31,6 +31,12 @@ describe("dataSorter.sortMultiple", () => { score: 5, label: "A5", }; + const obj6 = { + cat: 1, + ref: "1A", + score: 5, + label: "A5", + }; it("Should sort by cat ASC", () => { expect( @@ -53,9 +59,9 @@ describe("dataSorter.sortMultiple", () => { }); it("Should sort by ref number or string by ASC", () => { - const x = ["1", "1", "2", 3, "10"]; + const x = ["1", "1", "1A", "2", 3, "10"]; const expected = sortMultiple( - [obj1, obj4, obj2, obj5, obj3], + [obj1, obj4, obj2, obj5, obj3, obj6], true, (item) => [item.ref] ).map((item) => item.ref); diff --git a/packages/common/src/search/sorter/dataSorter.service.ts b/packages/common/src/search/sorter/dataSorter.service.ts index 24da72b44f..41d46f7114 100644 --- a/packages/common/src/search/sorter/dataSorter.service.ts +++ b/packages/common/src/search/sorter/dataSorter.service.ts @@ -10,30 +10,24 @@ export const compareAttributes = ( if (a === null || a === undefined) return -1; if (b === null || b === undefined) return 1; } else { - // En tri descendant, null/undefined descendent en bas if (a === null || a === undefined) return 1; if (b === null || b === undefined) return -1; } - // Détecter si c'est une date valide + // Conversion immédiate en string + const strA = a === null || a === undefined ? "" : String(a); + const strB = b === null || b === undefined ? "" : String(b); + + // Pour les dates, garder le comportement spécial if (isValidDate(a) && isValidDate(b)) { - const dateA = new Date(a as string); - const dateB = new Date(b as string); + const dateA = new Date(strA); + const dateB = new Date(strB); return asc ? dateA.getTime() - dateB.getTime() : dateB.getTime() - dateA.getTime(); } - // Détecter si c'est un nombre ou une chaîne numérique - const numA = typeof a === "string" ? parseFloat(a) : Number(a); - const numB = typeof b === "string" ? parseFloat(b) : Number(b); - if (!isNaN(numA) && !isNaN(numB)) { - return asc ? numA - numB : numB - numA; - } - - // Par défaut, comparer comme du texte - const strA = String(a); - const strB = String(b); + // Comparaison simple en string return asc ? strA.localeCompare(strB) : strB.localeCompare(strA); }; diff --git a/packages/frontend/src/app/modules/manage-usagers/components/usager-filter/services/usagersSorter.service.spec.ts b/packages/frontend/src/app/modules/manage-usagers/components/usager-filter/services/usagersSorter.service.spec.ts index 0c2fb1f82e..7e346578ab 100644 --- a/packages/frontend/src/app/modules/manage-usagers/components/usager-filter/services/usagersSorter.service.spec.ts +++ b/packages/frontend/src/app/modules/manage-usagers/components/usager-filter/services/usagersSorter.service.spec.ts @@ -225,22 +225,22 @@ describe("usagersSorter", () => { }); describe("sortBy avec tri par ID (customRef)", () => { - it("devrait trier les références numériques avant les non-numériques en asc", () => { + it("devrait trier les 'ref' numériques avant les non-numériques en asc", () => { const result = usagersSorter.sortBy(baseUsagers, { sortKey: "ID", sortValue: "asc", }); - expect(result.map((u) => u.customRef)).toEqual(["001", "003", ""]); + expect(result.map((u) => u.ref)).toEqual([1, 2, 3]); }); - it("devrait trier les références numériques après les non-numériques en desc", () => { + it("devrait trier les 'ref' numériques après les non-numériques en desc", () => { const result = usagersSorter.sortBy(baseUsagers, { sortKey: "ID", sortValue: "desc", }); - expect(result.map((u) => u.customRef)).toEqual(["", "003", "001"]); + expect(result.map((u) => u.ref)).toEqual([3, 2, 1]); }); // Test avec un jeu de données plus complexe diff --git a/packages/frontend/src/app/modules/manage-usagers/components/usager-filter/services/usagersSorter.service.ts b/packages/frontend/src/app/modules/manage-usagers/components/usager-filter/services/usagersSorter.service.ts index 9bef2ee7e9..b57aea56d3 100644 --- a/packages/frontend/src/app/modules/manage-usagers/components/usager-filter/services/usagersSorter.service.ts +++ b/packages/frontend/src/app/modules/manage-usagers/components/usager-filter/services/usagersSorter.service.ts @@ -48,12 +48,15 @@ export function sortUsagersByCustomRef( asc: boolean ): UsagerLight[] { return sortMultiple(usagers, asc, (usager) => { - const customRef = usager.customRef?.trim() || `${usager.ref}`; - const isInteger = /^\d+$/.test(customRef); + const customRef = usager.customRef?.trim() || String(usager.ref); + const isNumeric = /^\d+$/.test(customRef); return [ - isInteger ? 0 : 1, - isInteger ? parseInt(customRef, 10) : customRef.toLowerCase(), + isNumeric ? 0 : 1, + isNumeric ? customRef.replace(/^0+/, "").length : 0, + + customRef.toLowerCase(), + usager.nom.toLowerCase(), usager.prenom.toLowerCase(), ];