Jump to content

How to sort table by key


maffius

Recommended Posts

Posted (edited)

How can I use table.sort to sort table "lista" in my script? I want to sort players in gridlist by their name.

 

function aktualizujKontakty()
    lista={}
    for i,v in ipairs(getElementsByType("player")) do
        if v~=localPlayer then
            lista[getPlayerName(v)]={v,getElementData(localPlayer,"PW:"..getPlayerName(v)) or ""}
        end
    end
end

function wyswietlKontakty()
    guiGridListClear(gridlist[1])
    for i,v in pairs(lista) do
        r=guiGridListAddRow(gridlist[1])
        guiGridListSetItemText(gridlist[1],r,1,skipColorCode(i),false,false)
        guiGridListSetItemData(gridlist[1],r,1,i)
        if nieodczytane[i] then
            guiGridListSetItemColor(gridlist[1],r,1,0,255,0)
        end
        if zablokowany[i] then
            guiGridListSetItemColor(gridlist[1],r,1,255,0,0)
        end
    end
end

 

Edited by maffius
Posted
1 minute ago, maffius said:

How can I use table.sort to sort table "lista" in my script? I want to sort players in gridlist by their name.

 


function aktualizujKontakty()
    lista={}
    for i,v in ipairs(getElementsByType("player")) do
        if v~=localPlayer then
            lista[getPlayerName(v)]={v,getElementData(localPlayer,"PW:"..getPlayerName(v)) or ""}
        end
    end
end

function wyswietlKontakty()
    guiGridListClear(gridlist[1])
    for i,v in pairs(lista) do
        r=guiGridListAddRow(gridlist[1])
        guiGridListSetItemText(gridlist[1],r,1,skipColorCode(i),false,false)
        guiGridListSetItemData(gridlist[1],r,1,i)
        if nieodczytane[i] then
            guiGridListSetItemColor(gridlist[1],r,1,0,255,0)
        end
        if zablokowany[i] then
            guiGridListSetItemColor(gridlist[1],r,1,255,0,0)
        end
    end
end

 

function pairsByKeys (t, f)
	local a = {}
	for n in pairs(t) do table.insert(a, n) end
		table.sort(a, f)
		local i = 0      -- iterator variable
		local iter = function ()   -- iterator function
		i = i + 1
		if a[i] == nil then return nil
			else return a[i], t[a[i]]
		end
	end
return iter
end

function aktualizujKontakty()
    lista={}
    for i,v in ipairs(getElementsByType("player")) do
        if v~=localPlayer then
            lista[getPlayerName(v)]={v,getElementData(localPlayer,"PW:"..getPlayerName(v)) or ""}
        end
    end
end

function wyswietlKontakty()
    guiGridListClear(gridlist[1])
    for i,v in pairsByKeys(lista) do -- see this line 
        r=guiGridListAddRow(gridlist[1])
        guiGridListSetItemText(gridlist[1],r,1,skipColorCode(i),false,false)
        guiGridListSetItemData(gridlist[1],r,1,i)
        if nieodczytane[i] then
            guiGridListSetItemColor(gridlist[1],r,1,0,255,0)
        end
        if zablokowany[i] then
            guiGridListSetItemColor(gridlist[1],r,1,255,0,0)
        end
    end
end

This will sort table by key and will add players automatically :)

  • Like 1
Posted

Works good, but it sorts wrong because when someone has nick "test" and few other players have nicks with first upper case then "test" will be last in the list :D

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