diff --git a/src/shared/utils/challenge-listing/sort.js b/src/shared/utils/challenge-listing/sort.js index 8588f29048..4ef586d1f8 100644 --- a/src/shared/utils/challenge-listing/sort.js +++ b/src/shared/utils/challenge-listing/sort.js @@ -3,7 +3,7 @@ */ import moment from 'moment'; -import { sumBy } from 'lodash'; +import { find, sumBy } from 'lodash'; export const SORTS = { CURRENT_PHASE: 'current-phase', @@ -25,7 +25,15 @@ export default { name: 'Current phase', }, [SORTS.MOST_RECENT]: { - func: (a, b) => moment(b.registrationStartDate).diff(a.registrationStartDate), + func: (a, b) => { + const getRegistrationStartDate = (challenge) => { + const registrationPhase = find(challenge.phases, p => p.name === 'Registration'); + return registrationPhase.actualStartDate || registrationPhase.scheduledStartDate; + }; + const aRegistrationStartDate = getRegistrationStartDate(a); + const bRegistrationStartDate = getRegistrationStartDate(b); + return moment(bRegistrationStartDate).diff(aRegistrationStartDate); + }, name: 'Most recent', }, [SORTS.NUM_REGISTRANTS]: { @@ -42,8 +50,13 @@ export default { }, [SORTS.TIME_TO_REGISTER]: { func: (a, b) => { - const aDate = moment(a.registrationEndDate || a.submissionEndTimestamp); - const bDate = moment(b.registrationEndDate || b.submissionEndTimestamp); + const getRegistrationEndDate = (challenge) => { + const registrationPhase = find(challenge.phases, p => p.name === 'Registration'); + return registrationPhase.actualEndDate || registrationPhase.scheduledEndDate; + }; + + const aDate = moment(getRegistrationEndDate(a) || a.submissionEndTimestamp); + const bDate = moment(getRegistrationEndDate(b) || b.submissionEndTimestamp); if (aDate.isBefore() && bDate.isAfter()) return 1; if (aDate.isAfter() && bDate.isBefore()) return -1;