Skip to content

Commit

Permalink
fix: birthDay sort (#299)
Browse files Browse the repository at this point in the history
  • Loading branch information
Himali-Malvawala authored Dec 26, 2024
1 parent 6d26024 commit b96e89c
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion src/people/components/PeopleSearchResults.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand Down

0 comments on commit b96e89c

Please sign in to comment.