table.sort() question

Here is the link for the API reference: https://docs.coronalabs.com/api/library/table/sort.html

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.

Thanks guys 

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 ) &nbsp;&nbsp;&nbsp; return a.Distance \< b.Distance end

or something like that.

Rob

It is on the docs:

This compare function receives two arguments and must return true if the first argument should come first in the sorted array

So, for you case it would be:

local function compare( a, b ) return a.Distance \< b.Distance end table.sort( spots, compare )

Thanks guys, I appreciate the help

ok, so I tested the solutions you guys provided by putting this into my code after the table

local function compare( a, b ) return a.Distance \< b.distance end spots.sort( spots, compare )

but then I get an error that states “Attempt to call field ‘sort’ (a nil value)”

I can’t image what would be causing this issue…

im an idiot… I should have put table.sort( spots, compare)… Sorry guys

I’d check my case too:

return a.Distance \< b.distance

versus

return a.Distance \< b.Distance

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 ) &nbsp;&nbsp;&nbsp; return a.Distance \< b.Distance end

or something like that.

Rob

It is on the docs:

This compare function receives two arguments and must return true if the first argument should come first in the sorted array

So, for you case it would be:

local function compare( a, b ) return a.Distance \< b.Distance end table.sort( spots, compare )

Thanks guys, I appreciate the help

ok, so I tested the solutions you guys provided by putting this into my code after the table

local function compare( a, b ) return a.Distance \< b.distance end spots.sort( spots, compare )

but then I get an error that states “Attempt to call field ‘sort’ (a nil value)”

I can’t image what would be causing this issue…

im an idiot… I should have put table.sort( spots, compare)… Sorry guys

I’d check my case too:

return a.Distance \< b.distance

versus

return a.Distance \< b.Distance