From c7405c36d8be7c7a08ad445e6462215d34612468 Mon Sep 17 00:00:00 2001 From: Finn Stutzenstein Date: Thu, 26 Nov 2020 11:39:43 +0100 Subject: [PATCH] Fix sorting of assignment options --- .../services/assignment-poll.service.ts | 27 +++++++++++++------ 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/client/src/app/site/assignments/modules/assignment-poll/services/assignment-poll.service.ts b/client/src/app/site/assignments/modules/assignment-poll/services/assignment-poll.service.ts index 939150391..237b3f5f3 100644 --- a/client/src/app/site/assignments/modules/assignment-poll/services/assignment-poll.service.ts +++ b/client/src/app/site/assignments/modules/assignment-poll/services/assignment-poll.service.ts @@ -117,19 +117,30 @@ export class AssignmentPollService extends PollService { const tableData: PollTableData[] = poll.options .sort((a, b) => { if (this.sortByVote) { + let compareValue; if (poll.pollmethod === AssignmentPollMethod.N) { - // most no on top: - // return b.no - a.no; // least no on top: - return a.no - b.no; + compareValue = a.no - b.no; } else { - return b.yes - a.yes; + // most yes on top + compareValue = b.yes - a.yes; } + + // Equal votes, sort by weight to have equal votes correctly sorted. + if (compareValue === 0 && a.weight && b.weight) { + // least weight on top + return a.weight - b.weight; + } else { + return compareValue; + } + } + + // PollData does not have weight, we need to rely on the order of things. + if (a.weight && b.weight) { + // least weight on top + return a.weight - b.weight; } else { - // PollData does not have weight, we need to rely on the order of things. - if (a.weight && b.weight) { - return b.weight - a.weight; - } + return 0; } }) .map((candidate: ViewAssignmentOption) => {