Jump to content

Order elements in a table


Recommended Posts

Posted

It is possible to order elements from a table

For example a players table, order players with team first, then players without team.

It is possible make that?

How?

"El conocimiento jamás debe detenerse, por que es lo único que nos salvará cuando no nos quede más".

Att: -|TG|-Mister[Q]<.

Posted

Try this:

function getAllPlayers() 
    local pTable = {} 
    local teams = getElementsByType("team") 
    if (not teams) then return end 
    for k, v in pairs(teams) do 
        local plrs = getPlayersInTeam(v) 
        if (not plrs) then return end 
        pTable[getTeamName(v)] = plrs 
    end 
    return pTable 
end 

You can call this function in other functions by typing getAllPlayers() and it will return a teable like this:

teams ={ 
['teamName'] = {plr, plr, plr, plr}, 
['otherTeam'] = {plr, plr, plr, plr} 
} 
-- Use it like this: 
for k, v in pairs(teams) do 
    for i, d in pairs(v) do 
        outputChatBox("Player "..getPlayerName(d).." is in team "..k) 
    end 
end 

or i made another function like this, it contains players without team names:

function getAllPlayersV2() 
    local pTable = {} 
    local teams = getElementsByType("team") 
    if (not teams) then return end 
    for k, v in pairs(teams) do 
        local plrs = getPlayersInTeam(v) 
        if (not plrs) then return end 
        for i, d in pairs(plrs) do 
            pTable[#pTable+1] = d 
        end 
    end 
    return pTable 
end 

You can call this function in other functions like getAllPlayersV2() and will get table like this:

teams = { 
[1] = plr, 
[2] = plr, 
[3] = plr, 
[4] = plr 
} 
-- And use it like this: 
for k, v in pairs(teams) do 
    outputChatBox(getPlayerName(v)) -- Will output players per team in a row. 
end 

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