I was looking at this function and had some questions about how to get it to work for my situation.
I have a table as follows
local spots = { [1] {X = 100, Y = 100, Distance = 350}, [2] {X = 75, Y = 150, Distance = 450}, [3] {X = 150, Y = 200, Distance = 100}, [4] {X = 100, Y = 120, Distance = 200}, }
The table.sort() function API doesn’t seem to go into painful detail about how to set up the function.
I would like for it to look at my table and compare the Distance values and sort them in ascending order, so the smallest distance is in spots[1].
I was hoping maybe someone could explain a little bit about the API page and explain to me what the “a, b” would reference in the example and how I could set up the function to get it to sort depending on the distance value.
The idea for the sort function is you will get two values passed to it, the items to compare. Sort looks at two table rows at a time to decide which is the larger value. In your case, you’re passing in tables for each row. Something like:
local function compare( a, b ) return a.Distance \< b.Distance end
The idea for the sort function is you will get two values passed to it, the items to compare. Sort looks at two table rows at a time to decide which is the larger value. In your case, you’re passing in tables for each row. Something like:
local function compare( a, b ) return a.Distance \< b.Distance end