Posted 26 March 2013 - 07:47 PM
I am attempting to sort a table by a sub sub-table, however it seems that the comparison function i pass in to "table.sort" is never called. This is for a high score system, i wanted to write it so i could call scores["playerName"]["bestTime"] but when I attempt to sort the list to print it in a top 10 list It does not display correctly.
Here is a simplified version of what i am doing,
if everything worked correctly I should get
My initial guess is LUA tables act like C# dictionaries, so order is not guaranteed. If that is so what is a good design pattern so I can do quick updates but also sort out a top 10 list based on a sub-table?
(pastebin of real program http://pastebin.com/DcGiEwYd)
Here is a simplified version of what i am doing,
local testString = "{[\"leftler\"]={[\"tries\"]=1,[\"bestTime\"]=1.000,},[\"leftler2\"]={[\"tries\"]=2,[\"bestTime\"]=3.000,},[\"leftler3\"]={[\"tries\"]=3,[\"bestTime\"]=2.000,},}"
local testTable = textutils.unserialize(testString)
function compareTimes(player1, player2)
print("sorting")
player1Time = player1["bestTime"]
player2Time = player1["bestTime"]
--if the player does not have a best time use the math.huge value for sorting
if(player1Time == nil) then
player1Time = math.huge
end
if(player2Time == nil) then
player2Time = math.huge
end
print("time1: "..player1Time..", time2: "..player2Time)
return player1Time < player2Time
end
function formatTime(minecraftTime)
local realSeconds = minecraftTime * 50
local totalHours, totalMin, totalSec
totalHours = realSeconds / 60 / 60
totalMin = (realSeconds / 60) % 60
totalSec = realSeconds % 60
return totalHours, totalMin, totalSec
end
table.sort(testTable, compareTimes)
local currentIndex = nil
local done = false
for i=1, 10 do
currentIndex = next(testTable,currentIndex)
if(currentIndex ~= nil and done == false) then
local hr, mn, sec = formatTime(testTable[currentIndex]["bestTime"])
print(string.format("%2d) %s\t%d:%02d:%02d", i, currentIndex, hr, mn, sec))
else
done = true
fillLine(" ")
end
end
if everything worked correctly I should get
1) leftler 0:00:50
2) leftler3 0:01:40
3) leftler2 0:02:40
however my code is outputing
1) leftler3 0:01:40
2) leftler 0:00:50
3) leftler2 0:02:40
My initial guess is LUA tables act like C# dictionaries, so order is not guaranteed. If that is so what is a good design pattern so I can do quick updates but also sort out a top 10 list based on a sub-table?
(pastebin of real program http://pastebin.com/DcGiEwYd)