LabiVila Posted August 1, 2015 Share Posted August 1, 2015 Hey, so if I've got something like: mytable = {} table.insert (mytable, {source = player, number = number}) how can I sort the table according to highest number going down? I mean something like doing a top10 or top 15. I tried to loop through it and do the function a > b but didn't result as wished. What can I do? Link to comment
Sasu Posted August 1, 2015 Share Posted August 1, 2015 table.sort(mytable, function(a,b) return a[2] > b[2] end) Link to comment
LabiVila Posted August 1, 2015 Author Share Posted August 1, 2015 local theTable = {} function top (who) theTable = who showCursor (true) local main = guiCreateWindow (0.25, 0.15, 0.5, 0.7, "Top points table", true) local mainList = guiCreateGridList (0.04, 0.06, 0.92, 0.94, true, main) local mainListColumn = guiGridListAddColumn (mainList, "Account owner", 0.45) local mainListColumnPoints = guiGridListAddColumn (mainList, "Points", 0.45) if mainListColumn then for i,j in pairs (theTable) do table.sort (theTable, function (a,b) return a[2] > b[2] end) local mainListRow = guiGridListAddRow (mainList) local points = j.pts local name = j.acc guiGridListSetItemText (mainList, mainListRow, mainListColumn, i..". "..j.acc, false, false) guiGridListSetItemText (mainList, mainListRow, mainListColumnPoints, " "..j.pts, false, false) end end end addEvent ("top15pts", true) addEventHandler ("top15pts", getRootElement(), top) Attempt to compare two nil values Link to comment
JR10 Posted August 1, 2015 Share Posted August 1, 2015 table.sort(mytable, function(a,b) return a.number > b.number end) Link to comment
LabiVila Posted August 1, 2015 Author Share Posted August 1, 2015 Working like charm, thank you so much Link to comment
ixjf Posted August 1, 2015 Share Posted August 1, 2015 Ew, no. You're sorting the table for each entry in the table. Sort it before the loop. Link to comment
LabiVila Posted August 1, 2015 Author Share Posted August 1, 2015 Yea did that already, thanks anyway Link to comment
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now