Rearrange a table

I have a table of vec2s in random order. What I want to do is change it to a table of vec2s in a specific order. All the x values should be in ascending order, and if some x values are same then the y values among those should be in ascending order. I’m not really getting how to do it. Here’s what I’ve tried. You might have to run it twice or thrice cause it gives a correct order sometimes, just by luck.


--# Main
function setup()
    t={}
    for i=1,10 do
        t[i]=vec2(math.random(1,9),math.random(1,9))
    end
    
    print("Original")
    
    for k,v in pairs(t) do
        print(v)
    end
    for i=1,#t do
        for k,v in pairs(t) do
            if v.x>i then
                table.remove(t,k)
                table.insert(t,1,v)
            end
            if i==9 then
                table.remove(t,k)
                table.insert(t,1,v)
            end
        end
    end
    --how can i do it?
    for k,v in pairs(t) do
        for i=1,#t do
            if v.x==t[i].x then
                if k<i then
                    if v.y>t[i].y then
                        table.remove(t,k)
                        table.insert(t,k,v)-- I insert back at k cause now the table has one less value.
                    end
                end
            end
        end
    end
    --
    print("New")
    for k,v in pairs(t) do
        print(v)
    end
end

use sort

t={}
    for i=1,10 do
        t[i]=vec2(math.random(1,9),math.random(1,9))    
    end
    table.sort(t,function(a,b) if a.x<b.x then return true 
             elseif a.x==b.x and a.y<=b.y then return true end end)
    for i=1,10 do
       print(t[i].x,t[i].y) 
    end

Thanks didn’t know of that command.
Is there any place where ill find all codea commands and functions without knowing about them. Cause in the search function you need to know there exists a function like it to search for it.

that’s a Lua command. Just google for the Lua math, string and table libraries

@Saurabh the “eye” button on the keyboard does contain all of the Codea commands, as well as many of Lua’s libraries.

table.sort is documented in the Codea documentation here http://twolivesleft.com/Codea/Reference/Lua.html#table.sort — as well as in the in-app reference.

Thanks both of you!

@Simeon Not everything is documented in there, though…

@SkyTheCoder that’s correct. I’ll need to add more Lua libraries and functions to the documentation.

@Saurabh If you want to know all the functions, variables, tables, etc. used in Codea, run this program. When you run it, it will run for about 1 second and then return to the editor. There should be a tab created named “info153” that contains an alphabetical list of names and what they’re used for. I don’t think this shows 100% of the Codea stuff, but it’s close. The name “info153” is for the latest Codea Version 1.5.3 .


local t={}
local done=false
local readTable

function setup()
    readTable("",_G,0)    -- read contents of _G
    table.sort(t)         -- sort table
    local str="--[[\
"..table.concat(t,"\
").."\
--]]\
"
    saveProjectTab("info153",str)    -- create tab
    done=true
end

function draw()
    background(40,40,50)
    if done then    -- tab created
        close()     -- return to editor
    end
end

function touched(t)    -- added so function shows in list
end

function collide(c)    -- added so function shows in list
end

function readTable(val,tab,depth)
    local a,b
    depth=depth+1
    if depth>3 then    -- don't get in endless table loop
        table.insert(t,val..".....table")
        return
    end
    for a,b in pairs(tab) do    -- read contents of table
        if type(b)=="table" and b~=tab then
            if val=="" then
                readTable(a,b,depth)
            else
                readTable(val.."."..a,b,depth)
            end
        elseif val=="" then
            table.insert(t,a.."....."..type(b))
        else
            table.insert(t,val.."."..a.."....."..type(b))
        end
    end
end

Thanks it worked out @dave1707