Jump to content

Question: table stuff


LabiVila

Recommended Posts

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
  
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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...