Jump to content

Help Table


joaokennedy

Recommended Posts

Can anybody help me?


local Values = {["OB1"] = 8, ["OB2"] = 1, ["OB3"] = 10, ["OB4"] = 6}

I've tried it in all the ways I know and so far it has not worked.
I need this table to be organized by the values from highest to lowest, for example:
	local Values = {
	["OB3"] = 10, 
	["OB1"] = 8, 
	["OB4"] = 6}
	["OB2"] = 1, 

 

I have tried to use (table.sort) but I could not do the ranking from highest to lowest value. Could someone help me with this?

I also need a loop of the table where the first 3 values appear in the chat.

Link to comment

You could try using a set of pairs, but these would not be indexed by key.

local _set = {
  {"OB1", 8}, 
  {"OB2", 1},
  {"OB3", 10},
  {"OB4", 6}
}

table.sort(_set, 
  function(a, b)
    return a[2] > b[2] -- if pair a's 2nd value is greater than pair b's 2nd value, sort a above b
  end
)

-- After sorting:
-- set[1] = {"OB3", 10}
-- set[2] = {"OB1", 8}
-- set[3] = {"OB4", 6}
-- set[4] = {"OB2", 1}

 

Edited by MrTasty
  • 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...