How to get the index of a sorted table

Hi, I would like to sort a table so that I get, not the elements, but the indexes.

If my table is {5,3,1,2,4} how to get a table which contains {3,4,2,5,1} ?

I remember it was possible with the sort function of AS3 but with Lua I didn’t find.

Thanks to help me

Hi, That would be table.sort: http://docs.coronalabs.com/api/library/table/sort.html

Thanks but it’s not what I want. By the way I finally made myself :slight_smile: It just doesn’t work if there’re identical values :frowning:

local function sortIndex(t)     local tb1,tb2,tb3={},{},{}     for i=1,#t do tb1[tostring(t[i])]=i end     for key,value in pairs(tb1) do tb2[#tb2+1]=key end     table.sort(tb2)     for i=1,#tb2 do tb3[#tb3+1]=tb1[tb2[i]] end     return tb3 end local t1={5,3,1,2,4} local t2=sortIndex(t1) for i=1,#t2 do print(t2[i]) end

Hi, That would be table.sort: http://docs.coronalabs.com/api/library/table/sort.html

Thanks but it’s not what I want. By the way I finally made myself :slight_smile: It just doesn’t work if there’re identical values :frowning:

local function sortIndex(t)     local tb1,tb2,tb3={},{},{}     for i=1,#t do tb1[tostring(t[i])]=i end     for key,value in pairs(tb1) do tb2[#tb2+1]=key end     table.sort(tb2)     for i=1,#tb2 do tb3[#tb3+1]=tb1[tb2[i]] end     return tb3 end local t1={5,3,1,2,4} local t2=sortIndex(t1) for i=1,#t2 do print(t2[i]) end