Jump to content

[Help] table.sort


Snow-Man

Recommended Posts

hello everyone

i have a problem on my script that doesnt sort data when it gets new data into it

 

function getKillStats()
	table.sort(topKillers, function (a, b) return a[1] < b[1] end )
       	
	return topKillers
end

how i add the new data by this way

if topKillers[k] then
                    topKillers[k] = topKillers[k] + 1
                else
                    topKillers[k] = 1
                end

 

Link to comment

how your table looks like?

 

tkill = {{"aa", 10}, {"bb", 2}, {"cc", 5}}
-- Sort the table from high to low
table.sort(tkill, function (a, b) return a[2] > b[2] end)

for i, v in pairs(tkill) do
    print(v[1], v[2])
end
-- This will result:
-- aa, 10
-- cc, 5
-- bb, 2

function getKeyFromVal(name)
    if (not name) then
        return false
    end
    for i, v in pairs(tkill) do
        if (v[1] == name) then
           return i
        end
    end
    return false
end

function addkill(name, num)
    if (not name or not num) then
        return false
    end
    local key = getKeyFromVal(name)
    tkill[key][2] = tkill[key][2] + num
end
-- we add 6 kill to the "bb"
addkill("bb", 6)
-- sort again
table.sort(tkill, function (a, b) return a[2] > b[2] end)

for i, v in pairs(tkill) do
    print(v[1], v[2])
end
--This will result
-- aa, 10
-- bb, 8
-- cc, 5

 

Edited by NeverGiveup
Link to comment

@roaddogHello, thank you for helping i have used your example, it sorts the table from data i mean this

tkill = {{"aa", 10}, {"bb", 2}, {"cc", 5}}

but when add more data, it doesnt sort them, i have already made this to sort 

function sortKills()
	table.sort(topKillers, function (a, b) return a[2] > b[2] end)
	return topKillers
end	

and i tried to get the top killers on dx text like the following code 

for i, v in pairs ( sortKills() ) do
       		slot = slot + 1
       		if slot <= 5 then
	        	xy = xy + 51
	        	textKill = ("( "..tostring( v[2] ).." ) | "..tostring(( v[1] )) ) or "( N/A ) | N/A"
	        	dxDrawRectangle(8, 282+xy, 182, 49, tocolor(0, 0, 0, 175), false)
	        	dxDrawText(textKill , 9, 282+xy, 191, 331+xy, tocolor(255, 255, 255, 255), 1.25, "default-bold", "center", "center", false, false, false, false, false)
        
        	end
        end

 

so, in the final output i get this result 

( 10 ) | aa

( 5 ) | cc

( 2 ) | bb

( 7 ) | Ben

Link to comment
function addKill(name, num)
    if (not name or not num) then
        return false
    end
   	if (not topKillers[name] ) then 
    	topKillers[name] = {name, 0}

    end
    local key = getKeyFromVal(topKillers, name)
    if topKillers[key][1] == name then
	    topKillers[key][2] = topKillers[key][2] + num
	end
end

its already the same nothing changed, its only sorts the saved table

Link to comment

hello @Snow-Man welcome to the forum, I have prepared a code for your problem and tested it, you can solve your own problem by looking at my codes, I could not understand the code you prepared

 

local tkill = {{"aa", 10}, {"bb", 2}, {"cc", 5}}

addCommandHandler("adds", function()
   local test = {"bla", 404}
   table.insert(tkill, test)
end)

addCommandHandler("incr", function()
   tkill[2][2] = 405 
end)

addCommandHandler("show", function()
   table.sort(tkill, function (a, b) return a[2] > b[2] end)
   for i,_ in ipairs(tkill) do
      outputDebugString(tkill[i][1].." "..tkill[i][2])
   end
end)

 

6 hours ago, Snow-Man said:
function addKill(name, num)
    if (not name or not num) then
        return false
    end
   	if (not topKillers[name] ) then 
    	topKillers[name] = {name, 0}

    end
    local key = getKeyFromVal(topKillers, name)
    if topKillers[key][1] == name then
	    topKillers[key][2] = topKillers[key][2] + num
	end
end

its already the same nothing changed, its only sorts the saved table

this way you can constantly update, and do a short solution tests

 

local listUpdater = setTimer(function()
table.sort(tkill, function (a, b) return a[2] > b[2] end)
end, 300, 0)

 

  • Like 1
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...