feat: Modify sort with if/elseif/else randomize when the dates are equal (#10448)

Co-authored-by: Udit Takkar <53316345+Udit-takkar@users.noreply.github.com>
Co-authored-by: Keith Williams <keithwillcode@gmail.com>
Co-authored-by: Peer Richelsen <peeroke@gmail.com>
This commit is contained in:
Alex van Andel
2023-08-03 09:03:23 +00:00
committed by GitHub
co-authored by Udit Takkar Keith Williams Peer Richelsen
parent 1b6d1b3a5a
commit 2de4abba65
+4 -1
View File
@@ -92,7 +92,10 @@ async function leastRecentlyBookedUser<T extends Pick<User, "id" | "email">>({
}
const leastRecentlyBookedUser = availableUsers.sort((a, b) => {
return userIdAndAtCreatedPair[a.id] > userIdAndAtCreatedPair[b.id] ? 1 : -1;
if (userIdAndAtCreatedPair[a.id] > userIdAndAtCreatedPair[b.id]) return 1;
else if (userIdAndAtCreatedPair[a.id] < userIdAndAtCreatedPair[b.id]) return -1;
// if two (or more) dates are identical, we randomize the order
else return Math.random() > 0.5 ? 1 : -1;
})[0];
return leastRecentlyBookedUser;