Simple sort of table array
Posted: Fri Aug 19, 2016 3:42 pm
I can’t wrap my head around sorting a table constructed like so: pcs.A = {}
pcs.A[1] = { x, y, id } pcs.A[2] = { x, y, id } — values inside those brackets used to place a piece on a map at coordinates pcs.A[1], pcs.A[2].
The sorting function should order pcs.A by comparing distance to some origin x0, y0, derived from an external function that calculates kwikDist(pcs.A[1], pcs.A[2], x0, y0). I just don’t know the syntax to construct the sort, and of course, there’d be other tables that would need sorting too: pcs.B, pcs.C. The tables are pretty small (max 60 units) so speed is not a critical issue, but they are recreated every few minutes as the origin moves.
Sorting function: return kwikDist( pcs.A[1], pcs.A[2], xo, yo ) < kwikDist( pcs.A[i+1][1], pcs.A[i+1][2], xo, yo )
pcs.A[1] = { x, y, id } pcs.A[2] = { x, y, id } — values inside those brackets used to place a piece on a map at coordinates pcs.A[1], pcs.A[2].
The sorting function should order pcs.A by comparing distance to some origin x0, y0, derived from an external function that calculates kwikDist(pcs.A[1], pcs.A[2], x0, y0). I just don’t know the syntax to construct the sort, and of course, there’d be other tables that would need sorting too: pcs.B, pcs.C. The tables are pretty small (max 60 units) so speed is not a critical issue, but they are recreated every few minutes as the origin moves.
Sorting function: return kwikDist( pcs.A[1], pcs.A[2], xo, yo ) < kwikDist( pcs.A[i+1][1], pcs.A[i+1][2], xo, yo )