So how would i get the (1st,2nd,3rd) highest values from this table.
local Coins = { 1656343636363, 2000363646, 35462564642, 43587385730, 37504757030}
^^ Just wrote this out (still wanna know if it possible.)
Don't have any clue how to go about this.
Also would appreciate if someone helps.
Your title says that you want to get highest
number, for that you can use math.max, it returns highest
value from given values. Example
:
math.max(10, 5, 200) -- 200 math.max(0, 1, 2, 3, 4) -- 4 math.max(-50, 10, 500, 25, 300) -- 500
In your case you have table but it does not accept tables, you can fix it with unpack. It turns table into values that it returns it as a tuple, it only
returns values that have index
as a number
(In your case you don't need to worry). So you could
do:
local Highest = math.max(unpack(Coins))
This will return the highest
value from that table, the problem is that you said you want them to be in order
like 1st
, 2nd
... so here you should use table.sort! It sorts the table by using basic loop by given function. It accepts 2 arguments, the table
itself and function
, the function in your case does not have to be included because it automatically assigns the <
operator it. In your case it would look like this:
local Coins = { 1656343636363, 2000363646, 35462564642, 43587385730, 37504757030} table.sort(Coins)
It runs loop for every element which then picks random other element from the table and since i did not put any function there, it uses as i said <
operator so this would sort the table from lowest to highest
so the result would be:
[1] = 2000363646, [2] = 35462564642, [3] = 37504757030, [4] = 43587385730, [5] = 1656343636363
But if you want it to sort from highest to lowest
, you would have to make the custom
function in argument 2. All you have to do is swap <
with >
and it will be done:
local Coins = { 1656343636363, 2000363646, 35462564642, 43587385730, 37504757030} local function Sort(Number1, Number2) return Number1 > Number2 end table.sort(Coins, Sort)
The Number1
and Number2
are the random keys from the table it returns, you must use return because it returns bool
value and if the value is true
then it will sort the Number2
above Number1's index while if it is false
then it will sort it below the Number1
's index, this should return:
[1] = 1656343636363, [2] = 43587385730, [3] = 37504757030, [4] = 35462564642, [5] = 2000363646