# [Help] table.sort

## 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 )

end```

how i add the new data by this way

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

then you have to call table.sort everytime you add new data

Already tried that but didn't work

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

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

@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}}`

```function sortKills()
table.sort(topKillers, function (a, b) return a[2] > b[2] end)
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

okayy show me how you add the kill to the table

it should be check topKillers[key][1] == name and then topKillers[key][2] = topKillers[key][2] + 1

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

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

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

tkill[2][2] = 405
end)

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)```

• 1

@Shady1 thank you so much for helping, your example really made me understand the table insert, its really works well now

18 minutes ago, Snow-Man said:

@Shady1 thank you so much for helping, your example really made me understand the table insert, its really works well now

Please, if you have any questions, you can open a new topic and tag me, I will help you.

## Create an account

Register a new account