Jump to content

How to sort a table by value


AleksCore

Recommended Posts

Posted (edited)
table.sort(table, function(a, b) return a > b end )

Example:

function forbes ()
	local players = getElementsByType("players")
	local myTable = {}
	for index, value in ipairs(players) do
		local player = getPlayerName(value)
		local money = getPlayerMoney(value)
		table.insert(
			myTable,{
				name = player,
				value = money
				}
			)
	end
	table.sort(myTable, function(a, b) return a.value > b.value end )
end

 

Edited by Bananovy
  • Like 1
Posted (edited)
function getSortedTable( tableToSort ) --this function will sort our unsorted table and return sorted table
	local sortedTable = {}
  
	for player, money in ipairs(tableToSort) do
		local playerName = getPlayerName(player)
		table.insert(
			sortedTable,{
				['name'] = playerName,
				['value'] = money
				}
			)
	end
  
	table.sort(sortedTable, function(a, b) return a.value > b.value end )
  
	return sortedTable
end

function show10RichestPlayers()
	local sortedPlayers = getSortedTable(notSortedTable) --notSortedTable is our table with player element as a key and money number as value
	for i=1, 10 do
		outputChatBox(sortedPlayers[i].name.." - $"..sortedPlayers[i].value, root, 0, 255, 0)
	end
end
addCommandHandler('show10', show10RichestPlayers)

This is how I did it. Here could be mistakes, I didn't tested this code. Maybe for somebody this info will be useful

 

Edited by AleksCore
Posted

Try this:

function getSortedTable() 
	local sortedTable = {}
	local players = getElementsByType("player")
	for i=1, #players do
		local playerName = getPlayerName(players[i])
		local money = getPlayerMoney(players[i])
		table.insert(sortedTable,{['name'] = playerName,['value'] = money})
	end
  
	table.sort(sortedTable, function(a, b) return a.value > b.value end )
	return sortedTable
end

function show10RichestPlayers()
	local sortedPlayers = getSortedTable()
	for i=1, 10 do
		outputChatBox(sortedPlayers[i].name.." - $"..sortedPlayers[i].value, root, 0, 255, 0)
	end
end
addCommandHandler('show10', show10RichestPlayers)

 

Do not yield your back to your enemy, might feel something strange in your ass.

Two things are infinite the universe and human stupidity and i'm not sure about the universe.

UF: IsTextInGridList | GetGridListRowIndexFromText | Table.removeValue | removeHex | dxDrawTriangle

Skype: SaSuki102 | About Me | Youtube channel | Lua Tips & Tricks | Lua Strings | Lua Tables | Lua Operators

Posted
On 13.09.2016 at 1:59 AM, AleksCore said:

That's not exactly what I asked for. I want the key as a player and value as money amount

Anyways thanks, seems like I'll get how to do it myself.

I guess you can use this code:

	for player, money in ipairs(tableToSort) do
		table.insert(
			sortedTable,{
				p = player,
				v = money
				}
			)
	end

and

table.sort(sortedTable, function(a, b) return a.v > b.v end )

 I guess should be fine.

Posted (edited)
local players = getElementsByType('player')
table.sort(
	players,
	function(a, b)
		return getPlayerMoney(a) > getPlayerMoney(b)
	end
)

 

Edited by novo

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