Created
December 15, 2021 16:20
-
-
Save ryanlewis/8b5dfb1c699a959619cf55e0c45415d0 to your computer and use it in GitHub Desktop.
Sort a series of players by a rank into a series of teams
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const points = { | |
'immortal': 7, | |
'diamond': 6, | |
'platinum': 5, | |
'gold': 4, | |
'silver': 3, | |
'bronze': 2, | |
'iron': 1, | |
}; | |
const players = [ | |
{ name: 'a', rank: 'immortal' }, | |
{ name: 'b', rank: 'immortal' }, | |
{ name: 'c', rank: 'diamond' }, | |
{ name: 'd', rank: 'diamond' }, | |
{ name: 'e', rank: 'diamond' }, | |
{ name: 'f', rank: 'platinum' }, | |
{ name: 'g', rank: 'platinum' }, | |
{ name: 'h', rank: 'platinum' }, | |
{ name: 'i', rank: 'platinum' }, | |
{ name: 'j', rank: 'platinum' }, | |
{ name: 'k', rank: 'gold' }, | |
{ name: 'l', rank: 'gold' }, | |
{ name: 'm', rank: 'gold' }, | |
{ name: 'n', rank: 'gold' }, | |
{ name: 'p', rank: 'silver' }, | |
{ name: 'q', rank: 'silver' }, | |
{ name: 'r', rank: 'silver' }, | |
{ name: 's', rank: 'silver' }, | |
{ name: 't', rank: 'bronze' }, | |
{ name: 'u', rank: 'iron' }, | |
]; | |
const numTeams = players.length / 5; | |
console.log(`Creating ${numTeams} teams...`); | |
const teams = []; | |
for (let i = 0; i < numTeams; i++) { | |
teams.push([]); | |
} | |
const teamPoints = team => team.reduce((s, p) => s + points[p.rank], 0); | |
for (const player of players) { | |
const pv = points[player.rank]; | |
const lowestTeam = teams.sort((t1, t2) => { | |
const t1p = teamPoints(t1); | |
const t2p = teamPoints(t2); | |
return t1p - t2p; | |
})[0]; | |
lowestTeam.push(player); | |
} | |
let teamIndex = 1; | |
for (const team of teams) { | |
console.log(`Team ${teamIndex++} (${teamPoints(team)} points):`); | |
for (const player of team) { | |
console.log(` ${player.name} - ${player.rank}`); | |
} | |
console.log(''); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment