# Table Sorting Problem

I want to sort a table from this
`{ {1,2,4}, {1,2,3}, {1,2,10}, {1,2,5}, {2,2,4}, {1,2,3}, {2,2,10}, {2,2,5} }`
into
`{ {1,2,3}, {1,2,4}, {1,2,5}, {1,2,10}, {2,2,3}, {1,2,4}, {2,2,5}, {2,2,10} }`
basically coordinates ranging from x,y,z. I want to sort it based on its x value then its z.

I’m having a hardtime thinking about doing this. I know insertion methods are a way, but i can’t think properly right now.

You can also just use table.sort.

``````table.sort(t, function(a, b)
local ax, ay, az = a[1], a[2], a[3]
local bx, by, bz = b[1], b[2], b[3]
if ax < bx then return true end
if ax > bx then return false end
if az < bz then return true end
if az > bz then return false end
return ay < by
end)

local t = { {1,2,4}, {1,2,3}, {1,2,10}, {1,2,5}, {2,2,4}, {1,2,3}, {2,2,10}, {2,2,5} }
table.sort(t)
print(t)
``````

Instead of changing the way the tables arranged you could just change the way it’s constructed so you’ll always have X Y and Z but when you go to apply it you can switch it around to x z and y

Omg, thank you so much. I tried table.sort but i wasn’t really fully aware of how to use it.

1 Like

If I don’t forget tonight around 9:00 p.m. Eastern Standard Time I have a Vector3 converter that’s used to store data stores and then pull them to read afterwards requiring of construction functions

You can sort by the last element using `sortByLastElement` function.
`sortByLastElement = SortBy[#, Last] &`

``` ```

```sortByLastElement@{{1, 2, 4}, {1, 2, 3}, {1, 2, 10}, {1, 2, 5}, {2, 2, 4}, {1, 2, 3}, {2, 2, 10}, {2, 2, 5}} ```

{{1, 2, 3}, {1, 2, 3}, {1, 2, 4}, {2, 2, 4}, {1, 2, 5}, {2, 2, 5}, {1, 2, 10}, {2, 2, 10}}