Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: birthDay sort #299

Merged
merged 1 commit into from
Dec 26, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading