Most efficiently best way to get the smallest numeric value in a table while also taking its index?

I searched up lots of stuff to find best way to get the lowest numeric value of anything and while it is no doubt not that hard to do, I want to also get its index which its index is a string value like this: ["PlayerName"]. I do know how to do this in some of my own ways, for example iterating over an array where each value in the array is a table with the index value and numeric value which I can just look for the smallest number in and then extract the index value that was found with that numeric value.

In my example script, I have a function and want to output the index first and the numeric value second, while pretending we don’t know what’s in the table but we do know it should output Player2 and 19 since its the index with the smallest value.

local tableExample = {
["Player1"] = 59,
["Player3"] = 959,
["Player2"] = 19,
["ArtFoundation"] = 77,
["Player4"] = 21
}

function getSmallestNumWithIndex()

end
print(getSmallestNumWithIndex) -- should return Player2 and 19

I just want to know if there is a way more efficient than mine. I tried using math.min() since an article mentioned it being useful for a tuple of arguments (math.min(unpack(Example))) but it just returned an error of argument #1 being expected a number.

I am also curious if you could use table.sort() then look for the first or last values since that might help me with this post.

2 Likes

Pretty sure table.sort is what your looking for. You can use table.sort to arrange the values from biggest to smallest.

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

If you use it like this, the first index in the table will be the highest value and the last index will be the smallest. To get the smallest, you can do: table[#table] which returns the last value. To get the index from the value, I would use table.find as it returns the index if the value occurs.

table[#table] would return nil because you are trying to index a number and a table with string indexes cannot have its amount of values evaluated with # since doing #table for that kind of table will return 0 even if it has values in it.

2 Likes

I’m quite confused as to what your question is…
So are you looking for a way to find the smallest numeric value inside a table, given that all the objects are integers?

Actually sorry for not putting in decimal values but I want all numeric values no matter integer or decimal. I won’t be using negative numbers however.

Ah your right. So you should probably loop through the table and set a number for low. Change the number if it’s smaller than low.

function getLowest()
local low = math.huge
local index
for i, v in pairs(table) do
if v < low then
low = v
index = i
end
end
return index
end

7 Likes

There are a lot of ways to go about this, 2 of which were already given to you through this discussion. The general idea however would just be to compare all the values of a given table and determine which one has the lowest numeric value

This is what I was looking for. I didn’t want to make table.sort() and then do all kinds of complicated unnecessary code that can be simplified to this function. Though, I feel a little dumb for not thinking of this.

2 Likes

Also keep in mind that a lua function can have multiple return values, so OP can get both the index and the value from the same function

local function getLowest()
  -- your code as above
return index, low

local returnedIndex, returnedLow = getLowest()