From 14bee4bbb112ddab53914fa777be6004debeb5c4 Mon Sep 17 00:00:00 2001 From: Daniel Karbach Date: Sun, 13 Mar 2022 21:41:22 +0100 Subject: [PATCH] sort results by time --- resources/js/components/results/List.js | 3 ++- resources/js/helpers/Participant.js | 27 +++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/resources/js/components/results/List.js b/resources/js/components/results/List.js index 11eb9a8..448736a 100644 --- a/resources/js/components/results/List.js +++ b/resources/js/components/results/List.js @@ -2,9 +2,10 @@ import PropTypes from 'prop-types'; import React from 'react'; import Item from './Item'; +import { sortByResult } from '../../helpers/Participant'; const List = ({ round, tournament }) =>
- {tournament.participants.map(participant => + {sortByResult(tournament.participants, round).map(participant => (a, b) => { + const a_result = findResult(a, round); + const b_result = findResult(b, round); + const a_time = a_result ? a_result.time : 0; + const b_time = b_result ? b_result.time : 0; + if (a_time) { + if (b_time) { + if (a_time < b_time) return -1; + if (b_time < a_time) return 1; + return 0; + } + return -1; + } + if (b_time) { + return 1; + } + return 0; +}; + export const compareUsername = (a, b) => { const a_name = a && a.user && a.user.username ? a.user.username : ''; const b_name = b && b.user && b.user.username ? b.user.username : ''; @@ -10,7 +29,15 @@ export const findResult = (participant, round) => { return round.results.find(result => result.user_id === participant.user_id); }; +export const sortByResult = (participants, round) => { + if (!participants || !participants.length) return participants; + if (!round || !round.results || !round.results.length) return participants; + return participants.sort(compareResult(round)); +}; + export default { + compareResult, compareUsername, findResult, + sortByResult, }; -- 2.39.2