LabiVila Posted August 1, 2015 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?
Sasu Posted August 1, 2015 Posted August 1, 2015 table.sort(mytable, function(a,b) return a[2] > b[2] end) State: Inactive
LabiVila Posted August 1, 2015 Author 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
JR10 Posted August 1, 2015 Posted August 1, 2015 table.sort(mytable, function(a,b) return a.number > b.number end) Business System viewtopic.php?f=108&t=35797 Notepad++ Syntax Highlighting & Auto Completion viewtopic.php?f=91&t=76726 SQLite Tutorial viewtopic.php?f=148&t=38203
ixjf Posted August 1, 2015 Posted August 1, 2015 Ew, no. You're sorting the table for each entry in the table. Sort it before the loop. I used to know how to code, but then I took an arrow in the knee. Project Redivivus - Remaking Old School MTA With New Code MTA 0.6 Nightly 1 released
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