Skip to content

Commit

Permalink
fix(frontend): fix sort of ref
Browse files Browse the repository at this point in the history
  • Loading branch information
pYassine committed Dec 17, 2024
1 parent 55b54c3 commit 2aa6fa0
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 24 deletions.
10 changes: 8 additions & 2 deletions packages/common/src/search/sorter/dataSorter.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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);
Expand Down
22 changes: 8 additions & 14 deletions packages/common/src/search/sorter/dataSorter.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
];
Expand Down

0 comments on commit 2aa6fa0

Please sign in to comment.