• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!
  • 2026 staff recruitment is open! Check it out and consider applying!

LUA: Sort table

andu

Sold 649 scripts, 25 maps and 9 events!
Joined
Aug 7, 2009
Messages
978
Solutions
17
Reaction score
373
GitHub
olrios
Twitch
jamagowy
Tried to sort it but always i have result = headache :D
Give me a code what will sort this table by highest chance to lowest, please.

LUA:
local table = {
	[1] = {item = {8982, 1}, chance = 1},
	[2] = {item = {8985, 1}, chance = 80},
	[3] = {item = {9006, 1}, chance = 45},
	[4] = {item = {9019, 1}, chance = 1},
	[5] = {item = {9662, 1}, chance = 300}

Thanks in advance
 
Solution
Code:
local t = {
    [1] = {item = {8982, 1}, chance = 1},
    [2] = {item = {8985, 1}, chance = 80},
    [3] = {item = {9006, 1}, chance = 45},
    [4] = {item = {9019, 1}, chance = 1},
    [5] = {item = {9662, 1}, chance = 300}
}


table.sort(t, function(x, y) return x.chance > y.chance end)


for i, d in ipairs(t) do
    print('['..i..'] = {item = {'.. d.item[1]..', '..d.item[2]..'}, chance = ' .. d.chance .. '},')
end

Code:
>lua -e "io.stdout:setvbuf 'no'" "lua.lua" 
[1] = {item = {9662, 1}, chance = 300},
[2] = {item = {8985, 1}, chance = 80},
[3] = {item = {9006, 1}, chance = 45},
[4] = {item = {9019, 1}, chance = 1},
[5] = {item = {8982, 1}, chance = 1},
>Exit code: 0
Code:
local t = {
    [1] = {item = {8982, 1}, chance = 1},
    [2] = {item = {8985, 1}, chance = 80},
    [3] = {item = {9006, 1}, chance = 45},
    [4] = {item = {9019, 1}, chance = 1},
    [5] = {item = {9662, 1}, chance = 300}
}


table.sort(t, function(x, y) return x.chance > y.chance end)


for i, d in ipairs(t) do
    print('['..i..'] = {item = {'.. d.item[1]..', '..d.item[2]..'}, chance = ' .. d.chance .. '},')
end

Code:
>lua -e "io.stdout:setvbuf 'no'" "lua.lua" 
[1] = {item = {9662, 1}, chance = 300},
[2] = {item = {8985, 1}, chance = 80},
[3] = {item = {9006, 1}, chance = 45},
[4] = {item = {9019, 1}, chance = 1},
[5] = {item = {8982, 1}, chance = 1},
>Exit code: 0
 
Solution
Code:
local t = {
    [1] = {item = {8982, 1}, chance = 1},
    [2] = {item = {8985, 1}, chance = 80},
    [3] = {item = {9006, 1}, chance = 45},
    [4] = {item = {9019, 1}, chance = 1},
    [5] = {item = {9662, 1}, chance = 300}
}


table.sort(t, function(x, y) return x.chance > y.chance end)


for i, d in ipairs(t) do
    print('['..i..'] = {item = {'.. d.item[1]..', '..d.item[2]..'}, chance = ' .. d.chance .. '},')
end

Code:
>lua -e "io.stdout:setvbuf 'no'" "lua.lua" 
[1] = {item = {9662, 1}, chance = 300},
[2] = {item = {8985, 1}, chance = 80},
[3] = {item = {9006, 1}, chance = 45},
[4] = {item = {9019, 1}, chance = 1},
[5] = {item = {8982, 1}, chance = 1},
>Exit code: 0

table.sort() also changes indexes, just reminding. :)
 
Last edited:
Back
Top