Sorting Tables of Tables
Posted: Sat Mar 19, 2011 7:57 pm
I am very frustrated trying to find a function that won't make my tiles in my isometric game look like this:
And I need a function that will sort the tables by the third value, greatest to least, and the tables that have equal third values will be sorted by their second values,least to greatest. Example:
{grass,0,3}
{grass,0,2}
{grass,3,1}
{grass,4,1}
{grass,5,1}
{grass,0,0}
I just can't seem to find the right math to do this, and it'd be cool if someone could help me.
The game draws the tile images via a table of tables, as the title states:
Code: Select all
sX = 0
sY = 0
tiles = {}
grass = love.graphics.newImage("grass.png")
forest = love.graphics.newImage("forest.png")
table.insert(tiles,{grass,0,0})
table.insert(tiles,{forest,0,1})
table.insert(tiles,{forest,0,-1})
table.insert(tiles,{forest,1,0})
table.insert(tiles,{forest,-1,0})
function love.draw()
for i,v in pairs(tiles) do
local img = v[1]
local x = v[2]
local y = v[3]
love.graphics.draw(img,
(sX+(x*28)+(y*28))+(400-32),
(sY+(x*14)+(y*-14))+(300-32)
)
end
end
{grass,0,3}
{grass,0,2}
{grass,3,1}
{grass,4,1}
{grass,5,1}
{grass,0,0}
I just can't seem to find the right math to do this, and it'd be cool if someone could help me.