How would I sort the following dictionary by highest number…
local Scores = {
Harry = 69,
Samuel = 52,
Barry = 47,
James = 70
}
…And then print it in a format like:
1. James (70), 2. Harry (69), 3. Samuel (52), 4. Barry (47)
How would I sort the following dictionary by highest number…
local Scores = {
Harry = 69,
Samuel = 52,
Barry = 47,
James = 70
}
…And then print it in a format like:
1. James (70), 2. Harry (69), 3. Samuel (52), 4. Barry (47)
The dictionary part of a Lua table has no notion of order. You can instead have arrays of dictionaries.
local scores = {
{ name = "Harry", score = 69 },
{ name = "Samuel", score = 52 },
{ name = "Barry", score = 47 },
{ name = "James", score = 70 }
}
table.sort(scores, function(a, b)
return a.score > b.score
end)
print(scores[1].name)