table.sort issues involving two variables

Hello one and all,
I am trying to make a system that prioritises the drawing of certain 2D objects based on how close they are to the camera.
In order to do this I have a table of values and each value is a representation of how close they are in number form. Then they are sorted into order. My issue is that I can’t find a way to store the old key, so that I can copy data across in order, as well as the distance value.
-Because of how table.sort works, I can’t use tables.
-I have tried using strings, but it puts 150 under 50 because it starts with a 1 as opposed to a 5.
-I can’t think of any other ways.
Please help!

@Athalax Can you give an example of what you’re trying to sort, a couple of entries.

Here is an example
{{1,1},{180,2},{10,3}}
I need it to be sorted by the first variable but maintain the second

@Athalax Here’s a way that works, but you need to modify your table data.

function setup()
    t={a1="1",a180="2",a10="3"}
    sortValueByKey(t)
end

function sortValueByKey(t)
    local tmp={}
    for a,b in pairs(t) do
        table.insert(tmp,a)
    end
    table.sort(tmp)
    for a,b in pairs(tmp) do
        print("key = "..b," value = "..t[b])
    end
end

@Athalax Actually, you don’t need the “ “ around the numbers.

function setup()
    t={a1=1,a180=2,a10=3}
    sortValueByKey(t)
end

function sortValueByKey(t)
    local tmp={}
    for a,b in pairs(t) do
        table.insert(tmp,a)
    end
    table.sort(tmp)
    for a,b in pairs(tmp) do
        print("key = "..b," value = "..t[b])
    end
end

Thanks

@Athalax Here’s another version if you want just numbers instead of a1, a180, etc.

function setup()
    t={[1]=1,[180]=2,[10]=3}
    sortValueByKey(t)
end

function sortValueByKey(t)
    local tmp={}
    for a,b in pairs(t) do
        table.insert(tmp,a)
    end
    table.sort(tmp)
    for a,b in pairs(tmp) do
        print("key = "..b," value = "..t[b])
    end
end

@Athalax If you don’t want to type all the [ ], here’s a version that will do it for you. Just create a simple table tab with the values you want.

function setup()
    tab={1,1,180,2,10,3}
    
    -- create table t using info in table tab.
    str="t={"
    for z=1,#tab,2 do
        str=str.."["..tab[z].."]="..tab[z+1]..","
    end
    str=str.."}" 
    print(str)      -- print string of table t
    loadstring(str)()
    
    sortValueByKey(t)
end

function sortValueByKey(t)
    local tmp={}
    for a,b in pairs(t) do
        table.insert(tmp,a)
    end
    table.sort(tmp)
    for a,b in pairs(tmp) do
        print("key = "..b," value = "..t[b])
    end
end

I tested it and it doesn’t work with identical items.
How would you do it with identical items?

The problem is if you have identical items, they both occupy the same key, so there’s only one of them. I’ll have to think about that.

@LoopSpace Where were you when he first asked the question. Your example solves everything. Actually I have something like that in my sort examples. I just didn’t look through enough of them.

table.sort can take a second parameter which is a sorting function.

t = {{1,1},{180,2},{10,3}}
table.sort(t, function(a,b) return a[1] < b[1] end )
    for k,v in ipairs(t) do
        print(v[1],v[2])
    end

Thanks for the helps

@dave1707 I was playing around with your craft project with the spheres!