Skip to content

Commit

Permalink
add sort fallback by id
Browse files Browse the repository at this point in the history
  • Loading branch information
diegoalzate committed May 27, 2024
1 parent b8b678e commit 3480585
Showing 1 changed file with 18 additions and 4 deletions.
22 changes: 18 additions & 4 deletions packages/berlin/src/pages/Cycle.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -211,15 +211,32 @@ function Cycle() {

const currentCycle = cycle?.forumQuestions[0];

const sortId = (a: QuestionOption, b: QuestionOption, order: Order) => {
const idA = a.id.toUpperCase();
const idB = b.id.toUpperCase();

return order === 'desc' ? idB.localeCompare(idA) : idA.localeCompare(idB);
};

const sortByLead = (a: QuestionOption, b: QuestionOption, order: Order) => {
const leadA = (a.user.lastName || a.user.username).toUpperCase();
const leadB = (b.user.lastName || b.user.username).toUpperCase();

if (leadA === leadB) {
return sortId(a, b, order);
}

return order === 'desc' ? leadB.localeCompare(leadA) : leadA.localeCompare(leadB);
};

const sortByAffiliation = (a: QuestionOption, b: QuestionOption, order: Order) => {
const affiliationA = a.user.group?.name.toUpperCase();
const affiliationB = b.user.group?.name.toUpperCase() ?? '';

if (affiliationA === affiliationB) {
return sortId(a, b, order);
}

return order === 'desc'
? affiliationB.localeCompare(affiliationA)
: affiliationA.localeCompare(affiliationB);
Expand All @@ -235,10 +252,7 @@ function Cycle() {
const votesB = localUserVotes?.find((vote) => vote.optionId === b.id)?.numOfVotes || 0;

if (votesA === votesB) {
const idA = a.id.toUpperCase();
const idB = b.id.toUpperCase();

return order === 'desc' ? idB.localeCompare(idA) : idA.localeCompare(idB);
return sortId(a, b, order);
}

return order === 'desc' ? votesB - votesA : votesA - votesB;
Expand Down

0 comments on commit 3480585

Please sign in to comment.