maffius Posted June 25, 2017 Share Posted June 25, 2017 (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 June 25, 2017 by maffius Link to comment
Ayush Rathore Posted June 25, 2017 Share Posted June 25, 2017 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 1 Link to comment
maffius Posted June 25, 2017 Author Share Posted June 25, 2017 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 Link to comment
maffius Posted June 25, 2017 Author Share Posted June 25, 2017 Nevermind i fixed it by myself Topic can be closed 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