From b96e89cfd33b9b4b0b16c3a328d363339756b950 Mon Sep 17 00:00:00 2001 From: Himali Malvawala <86650000+Himali-Malvawala@users.noreply.github.com> Date: Thu, 26 Dec 2024 12:09:41 +0530 Subject: [PATCH] fix: birthDay sort (#299) --- src/people/components/PeopleSearchResults.tsx | 23 ++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/src/people/components/PeopleSearchResults.tsx b/src/people/components/PeopleSearchResults.tsx index 659335c..1b60681 100644 --- a/src/people/components/PeopleSearchResults.tsx +++ b/src/people/components/PeopleSearchResults.tsx @@ -118,9 +118,30 @@ export function PeopleSearchResults(props: Props) { setSortDirection(!asc) //set sort direction for next time people = people.sort(function (a: any, b: any) { if (a[key] === null) return Infinity; // if value is null push to the end of array + if (key === "birthDay") { //there's no 'birthDay' property in the people object; instead use birthDate to sort + if (a["birthDate"] === null && b["birthDate"] === null) return 0; + if (a["birthDate"] === null) return 1; + if (b["birthDate"] === null) return -1; + } if (typeof a[key]?.getMonth === "function") { - return asc ? (a[key]?.getTime() - b[key]?.getTime()) : (b[key]?.getTime() - a[key]?.getTime()); + return asc ? (a[key] - b[key]) : (b[key] - a[key]); + } + + if (key === "birthDay") { //to sort dates as per the month + if (asc) { + if (a["birthDate"]?.getMonth() !== b["birthDate"]?.getMonth()) { + return a["birthDate"]?.getMonth() - b["birthDate"]?.getMonth(); + } else { + return a["birthDate"]?.getDate() - b["birthDate"]?.getDate(); + } + } else { + if (b["birthDate"]?.getMonth() !== a["birthDate"]?.getMonth()) { + return b["birthDate"]?.getMonth() - a["birthDate"]?.getMonth(); + } else { + return b["birthDate"]?.getDate() - a["birthDate"]?.getDate(); + } + } } const parsedNum = parseInt(a[key]);