I made the code underneath to sort my array and group people with the same score to show who all got 1st, 2nd and 3rd.
the "points" variable looks like this
points: {
'204255221017214977': 3,
'208993609255485441': 5,
'382197942762602496': 4,
'664606841409634324': 5,
'779857709155090432': 4,
'381084508276916225': 5,
'794200859612545075': 4,
'155149108183695360': 4,
'438978127973318656': 3,
'304028235082104843': 2,
'174548757151481856': 1
}
I was wondering if there is a much easier way to sort and put together the string like I did underneath with much easier code. Cuz I'll be honest this code looks and feels aweful.
example endresult:
:first_place: @Excanator had 5 points
:first_place: @zerow had 5 points
:first_place: @AnimeHotOwO had 5 points
:second_place: @christmas beans had 4 points
:second_place: @ExcavatorGames had 4 points
:second_place: @Dyno had 4 points
:second_place: @LogiBot Premium had 4 points
:third_place: @Restarter v3 had 3 points
:third_place: @YAGPDB.xyz had 3 points
@aNt had 2 points
@Kaori had 1 point
const sorted = Object.keys(points).sort((a, b) => {
return points[b] - points[a]
})
var results = `\n`
var firstPlace = points[sorted[0]]
var secondPlace = null
var thirdPlace = null
var leftover = null
for(const key of sorted) {
var amount = points[key]
if(leftover){
results += `<@${key}> had ${amount} point${amount === 1 ? '' : 's'}\n`
} else if(thirdPlace) {
if(thirdPlace === amount) {
results += `:third_place: <@${key}> had ${amount} point${amount === 1 ? '' : 's'}\n`
} else {
results += `\n`
results += `<@${key}> had ${amount} point${amount === 1 ? '' : 's'}\n`
leftover = amount
}
} else if(secondPlace) {
if(secondPlace === amount) {
results += `:second_place: <@${key}> had ${amount} point${amount === 1 ? '' : 's'}\n`
} else {
results += `\n`
results += `:third_place: <@${key}> had ${amount} point${amount === 1 ? '' : 's'}\n`
thirdPlace = amount
}
} else if(firstPlace) {
if(firstPlace === amount) {
results += `:first_place: <@${key}> had ${amount} point${amount === 1 ? '' : 's'}\n`
} else {
results += `\n`
results += `:second_place: <@${key}> had ${amount} point${amount === 1 ? '' : 's'}\n`
secondPlace = amount
}
}
}
```