AleksCore Posted September 12, 2016 Share Posted September 12, 2016 How to sort a table where key is a player(userdata) and value is money(number). How to sort by value? Thanks! Link to comment
Bananovy Posted September 12, 2016 Share Posted September 12, 2016 (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 September 12, 2016 by Bananovy 1 Link to comment
AleksCore Posted September 12, 2016 Author Share Posted September 12, 2016 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. Link to comment
AleksCore Posted September 13, 2016 Author Share Posted September 13, 2016 (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 September 13, 2016 by AleksCore Link to comment
Walid Posted September 13, 2016 Share Posted September 13, 2016 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) Link to comment
Bananovy Posted September 14, 2016 Share Posted September 14, 2016 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. Link to comment
novo Posted September 14, 2016 Share Posted September 14, 2016 (edited) local players = getElementsByType('player') table.sort( players, function(a, b) return getPlayerMoney(a) > getPlayerMoney(b) end ) Edited September 14, 2016 by novo 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