@@ -7,7 +7,7 @@ const Joi = require('joi');
7
7
8
8
const validator = require ( '@byba/express-validator' ) ;
9
9
10
- const { helpers : { SlugHelper : { formatSlug } } } = require ( __lib ) ;
10
+ const { helpers : { StringHelper : { formatSlug } , SortHelper } } = require ( __lib ) ;
11
11
12
12
const router = express . Router ( ) ;
13
13
@@ -84,43 +84,6 @@ async function recalculateEntryFees(db, season, typeID, org) {
84
84
}
85
85
}
86
86
87
- function shuffleArray ( array ) {
88
- for ( let i = array . length - 1 ; i > 0 ; i -- ) {
89
- const j = Math . floor ( Math . random ( ) * ( i + 1 ) ) ;
90
- [ array [ i ] , array [ j ] ] = [ array [ j ] , array [ i ] ] ;
91
- }
92
-
93
- return array ;
94
- }
95
-
96
- function leagueSort ( entries ) {
97
- entries . sort ( ( a , b ) => {
98
- const aMember = a . Organisation . OrganisationMemberships ;
99
- const aScore = aMember . length == 0 ? 0 : aMember [ 0 ] . LeagueScore ;
100
-
101
- const bMember = b . Organisation . OrganisationMemberships ;
102
- const bScore = bMember . length == 0 ? 0 : bMember [ 0 ] . LeagueScore ;
103
-
104
- return aScore - bScore ;
105
- } ) ;
106
-
107
- return entries ;
108
- }
109
-
110
- function entrySort ( array , direction ) {
111
- array . sort ( ( a , b ) => {
112
- const d1 = new Date ( a . EntryDate ) , d2 = new Date ( b . EntryDate ) ;
113
-
114
- if ( direction === 'asc' ) {
115
- return d1 - d2 ;
116
- }
117
-
118
- return d2 - d1 ;
119
- } ) ;
120
-
121
- return array ;
122
- }
123
-
124
87
function splitEntries ( entries , cutoff ) {
125
88
const late = [ ] , regular = [ ] ;
126
89
@@ -211,15 +174,15 @@ async function generateSchedule(req, next, eventID, config, divisionOrder) {
211
174
let late = [ ] , regular = div ;
212
175
if ( config . LateOnFirst ) {
213
176
[ late , regular ] = splitEntries ( div , cutoff ) ;
214
- entrySort ( late , 'desc' ) ;
177
+ late = late . sort ( ( a , b ) => SortHelper . dateProp ( 'EntryDate' , a , b , false ) ) ;
215
178
}
216
179
217
180
if ( config . Type . indexOf ( 'entry' ) > - 1 ) {
218
- entrySort ( regular , config . Type . split ( '-' ) [ 1 ] ) ;
181
+ regular = regular . sort ( ( a , b ) => SortHelper . dateProp ( 'EntryDate' , a , b , config . Type . split ( '-' ) [ 1 ] == 'asc' ) ) ;
219
182
} else if ( config . Type == 'league' ) {
220
- leagueSort ( regular ) ;
183
+ regular = regular . sort ( ( a , b ) => SortHelper . compare ( a . Organisation . OrganisationMemberships [ 0 ] ?. LeagueScore , b . Organisation . OrganisationMemberships [ 0 ] ?. LeagueScore ) ) ;
221
184
} else {
222
- shuffleArray ( regular ) ;
185
+ regular = SortHelper . shuffleArray ( regular ) ;
223
186
}
224
187
225
188
div = late . concat ( regular ) ;
@@ -272,7 +235,10 @@ router.get('/', async (req, res, next) => {
272
235
] ,
273
236
where : {
274
237
SeasonId : season . id
275
- }
238
+ } ,
239
+ order : [
240
+ [ 'Start' ]
241
+ ]
276
242
} ) ,
277
243
req . db . EventType . getActive ( )
278
244
] ) ;
0 commit comments