What is a,b in table.sort()

The question is I want to do some anaylsis system for my game, for that I need to know how to sort tables

local t = {15,7,66,93,10} -- table

table.sort(t, function(a,b)
	print(a..","..b)
end)

Now what is a,b in this? I thought a is a number,b is the next number of it

But turns out this is the output

-- 10,15
-- 66,15
-- 10,66
-- 7,66
-- 66,93

You see first number is 15 but it printed 10
Second is 7, it printed 15

So what is a,b in table.sort, what are the numbers it returned

A is the sorted value, B is the value that was there before the sort

Edit: I think

What you mean by sorted? It sorts number by number in a line right
It should print 15 as its the first number, but it printed 10

Also what is the number before the sort? all numbers r there in table

If you’re sorting it then it’ll sort in ASC order, meaning 10 would be the first value. So the number before the sort if 15 as that was the first number in the table

But,

local t = {15,7,66,93,10}
local total = 0
table.sort(t,function(a,b)
    print(t)
    print("a = previous number = "..a)
    print("b = current number = "..b)
    return a > b
end)

prints previous number as middle number
and current number as 7

a is not previous number, it will be next number in here right

I believe that the a and b are just 2 different numbers that are selected in the table (in such a way where every number can be sorted). If you return true, that number will be “sorted” (if it’s true, a will be before b in the table).

For example, in this code block:

local t = {1, 2, 3, 4, 5}

table.sort(t, function(a, b)
    return a < b -- If "a" is smaller than "b", "a" will appear before "b"
end)

print("\n")

for i, v in pairs(t) do
    print(i, v)
    --[[
        Notice that the table is now sorted from least to greatest
        Take note that during our table.sort function on line 3 - 5,
        we put "a" before "b" if "a" is smaller than "b", which
        caused the table to sort like this
    --]]
end

Output:

5   1
3   1
5   3
2   3
4   3
3   4
3   2
5   4
2   1


1   1
2   2
3   3
4   4
5   5
6 Likes