Shuubaru Posted November 28, 2016 Share Posted November 28, 2016 (edited) Hi, I need to find a way of sorting two tables with the same order: "kills" and "player names" tables, I manage to sort kills table by value but I don't know how to sort the player names table with the same order than kills table. Here is my code: --client side local screenW, screenH = guiGetScreenSize() --I use the colshapes to "simulate" players datas. col1 = createColSphere(0,0,0, 1) col2 = createColSphere(0,0,0, 1) col3 = createColSphere(0,0,0, 1) col4 = createColSphere(0,0,0, 1) col5 = createColSphere(0,0,0, 1) col6 = createColSphere(0,0,0, 1) col7 = createColSphere(0,0,0, 1) col8 = createColSphere(0,0,0, 1) col9 = createColSphere(0,0,0, 1) col10 = createColSphere(0,0,0, 1) col11 = createColSphere(0,0,0, 1) setElementData(col1, "kills.actual", 5) setElementData(col2, "kills.actual", 10) setElementData(col3, "kills.actual", 16) setElementData(col4, "kills.actual", 2) setElementData(col5, "kills.actual", 1) setElementData(col6, "kills.actual", 50) setElementData(col7, "kills.actual", 21) setElementData(col8, "kills.actual", 21) setElementData(col9, "kills.actual", 12) setElementData(col10, "kills.actual", 9) setElementData(col11, "kills.actual", 106) setElementData(col1, "kills.names", "test1") setElementData(col2, "kills.names", "test2") setElementData(col3, "kills.names", "test3") setElementData(col4, "kills.names", "test4") setElementData(col5, "kills.names", "test9") setElementData(col6, "kills.names", "test5") setElementData(col7, "kills.names", "test6") setElementData(col8, "kills.names", "#ff0000Akai") setElementData(col9, "kills.names", "#00ff00Green") setElementData(col10, "kills.names", "#0000ffBlue") setElementData(col11, "kills.names", "#ff00ff:O knows") function renderTabla() dxDrawImage(screenW - 318 - 10, (screenH - 198) / 2, 318, 198, "bgr.png", 0, 0, 0, tocolor(255, 255, 255, 255), false) dxDrawText("Kills on this round", screenW - 318 - 10, (screenH - 188) / 2, (screenW - 318 - 10) + 318, ( (screenH - 188) / 2) + 188, tocolor(255, 255, 255, 255), 0.66, "bankgothic", "center", "top", true, true, true, true, false) --sort the kills by hight value table.sort(tableKills, function (a,b) return tonumber(a) > tonumber (b) end ) if tableKills then for i=1,10 do posY = ((screenH-394)*0.81+(i*screenH*0.02)) dxDrawText(i.."- "..tableNames[i], (screenW - 308 - 10), posY, (screenW - 308 - 10) + 308, ( (screenH - 164) / 2) + 164, tocolor(255, 255, 255, 255), 1.00, "default", "left", "top", true, true, true, true, false) dxDrawText("Kills: "..tableKills[i], (screenW - 150 - 10), posY, (screenW - 308 - 10) + 150, ( (screenH - 164) / 2) + 164, tocolor(255, 255, 255, 255), 1.00, "default", "left", "top", true, true, true, true, false) end end end function testCols () tableKills = {} tableNames = {} --just pretending loop through players tablaCol = getElementsByType("colshape") for i=1,#getElementsByType("colshape") do colEle = getElementData(tablaCol[i], "kills.actual") colNom = getElementData(tablaCol[i], "kills.names") table.insert(tableKills, i, colEle) table.insert(tableNames, i, colNom) end setTimer(function() addEventHandler("onClientRender", getRootElement(), renderTabla) end, 3000, 1) end --for testing bindKey("5", "down", testCols) Please help! Edited November 28, 2016 by Shuuichi Link to comment
cacao Posted November 28, 2016 Share Posted November 28, 2016 (edited) -- https://regex101.com/ -- You need format your nicks name = "#ff0Ffffasd" matches = pregMatch(name, "^(.+)(#[0-9a-f]{6})(.+)$/i") -- name = #ff0Ffffasd -- matches[1] = #ff0Ffffasd -- matches[2] = #ff0Ffffasd -- matches[3] = #ff0Ffff if matches then name = matches[4] and matches[1] .. matches[4] or matches[3] -- why this construction? cause if we have asd#ffffffasd -- we need set nick "asdasd" end -- You take nick "asd", sort by this nick Edited November 28, 2016 by cacao Link to comment
Shuubaru Posted November 28, 2016 Author Share Posted November 28, 2016 12 hours ago, cacao said: -- https://regex101.com/ -- You need format your nicks name = "#ff0Ffffasd" matches = pregMatch(name, "^(.+)(#[0-9a-f]{6})(.+)$/i") -- name = #ff0Ffffasd -- matches[1] = #ff0Ffffasd -- matches[2] = #ff0Ffffasd -- matches[3] = #ff0Ffff if matches then name = matches[4] and matches[1] .. matches[4] or matches[3] -- why this construction? cause if we have asd#ffffffasd -- we need set nick "asdasd" end -- You take nick "asd", sort by this nick I think you misundertood. Or I explain it bad because my english is not enough good. What I wanted to do is order the nicks by them kills, I mean, if someone have the first place and 20 kills his nick will be first. Btw, I manage to do it just using another way (tables inside a table): function testCols () RankingData = {} tablaCol = getElementsByType("colshape") for i=1,#getElementsByType("colshape") do colEle = getElementData(tablaCol[i], "kills.actual") colNom = getElementData(tablaCol[i], "kills.names") table.insert(RankingData,{name=colNom,data=colEle}) end setTimer(function() table.sort(RankingData,function(a,b) return(tonumber(a.data) or 0 ) > ( tonumber(b.data) or 0 ) end ) refreshTopKills(RankingData) --after this I will retrieve all the datas by kills order end, 50, 1) setTimer(function() addEventHandler("onClientRender", getRootElement(), renderTabla) end, 100, 1) end bindKey("5", "down", testCols) function refreshTopKills( info ) showRanking = {} for i=1,#info do table.insert(showRanking, info[i]) end end I hope it will be useful for someone else. Link to comment
LoPollo Posted November 28, 2016 Share Posted November 28, 2016 If it's possible then assing an ID to each table value. Instead of inserting in the table the value, insert (in the table) a new table containing value and ID When sorting a table notice that you must use the value that is now in a sub-table. However i'm not experienced with sorting functions, so i can't help you with that. Link to comment
ViRuZGamiing Posted November 28, 2016 Share Posted November 28, 2016 Why not store it in SQL then you could do a select with an ORDER BY. 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