Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
How to sort lua tables with non-consecutive-integer keys? — Gideros Forum

How to sort lua tables with non-consecutive-integer keys?

MellsMells Guru
edited January 2013 in General questions
Hi,

I'm creating an array with non-consecutive-integer keys (1, 2, 3, 8, 23).
Here is an example below.

A Series
The A series are not sorted (I understand that, keys are strings) but how does lua store the keys?
The order is not even the one in which they were created.
>>Can we have control on the order the values are stored in an string-keys table?

B Series
I thought that the B series would be in order (numerical keys), but it's not.
>> How can I get the expected result for the array "b"?
local a = {}
a["1"] = 1
a["2"] = 2
a["3"] = 3
a["16"] = 4
a["26"] = 5
a["28"] = 6
 
local b = {}
b[1] = 1
b[2] = 2
b[3] = 3
b[16] = 4
b[20] = 5
b[23] = 6
b[26] = 7
b[28] = 8
 
local a2 = {}
a2["1"] = 1
a2["2"] = 2
a2["3"] = 3
a2["4"] = 4
a2["5"] = 5
a2["6"] = 6
 
local b2 = {}
b2[1] = 1
b2[2] = 2
b2[3] = 3
b2[4] = 4
b2[5] = 5
b2[6] = 6
 
 
local function printTable(table)
	local i, j
	for i, j in pairs(table) do
		print (i,":", j)
	end
	print ("=========")
end
 
printTable(a)
2	: 2
3	: 3
26	: 5
16	: 4
28	: 6
1	: 1
 
=========
-- ******************************************
-- This is where I would like more control
-- ******************************************
printTable(b)
1	: 1
2	: 2
3	: 3
26	: 7
23	: 6
20	: 5
28	: 8
16	: 4
 
 
=========
printTable(a2)
2	: 2
3	: 3
4	: 4
5	: 5
6	: 6
1	: 1
 
=========
printTable(b2)
1	: 1
2	: 2
3	: 3
4	: 4
5	: 5
6	: 6
 
=========
twitter@TheWindApps Artful applications : The Wind Forest. #art #japan #apps

Comments

  • twisttaptwisttap Member
    edited January 2013
    @Mells I think you should construct b like this:
    local b = {
    {1, 1},
    {2,2},
    {3,3},
    {16,4},
    {20,5},
    {23,6},
    {26,7} ,
    {28, 8}
    }
    Because a while ago, I had the same problem. Check this out:
    http://www.giderosmobile.com/forum/discussion/2244/how-to-output-pairs-in-order-#Item_5
  • @twisttap
    in your example how could you access a specific recipe?
    In my case (a calendar) I would like to be able to do :
    print (calendar[ 2013 ] [ 01 ] [ 24 ] ["status"])
    That's why I would like to avoid having the key and the value on the same level.
    twitter@TheWindApps Artful applications : The Wind Forest. #art #japan #apps
  • twisttaptwisttap Member
    edited January 2013
    @Mells, I take a look back to my project now and saw that I take a very different approach there. Instead of seperating the recipes, I created a big Table that holds all recipes but also assigned them an id, like:
    recipes = {
     
     
    	[1] = {name="Baguette", person=4, time=45, calorie=129, id=1},
    	[2] = {name="Cookies", person=4, time=45, calorie=307, id=1},
    ... etc
    }
    Are you trying to form a calendar ? If you are going to make something like a reminder etc, maybe you can try sqlite3 ?
  • @twisttap
    Yes kind of calendar so that's why it's important for me to keep using numerical keys and why I have non-consecutive integer keys in my table.
    twitter@TheWindApps Artful applications : The Wind Forest. #art #japan #apps
  • In this situation, I would think it would be a lot easier to create ids like @twisttap mentioned or even create a separate table that is only used for keeping track of what order they are currently in, like so:
    local a = {}
    a["1"] = 1
    a["2"] = 2
    a["3"] = 3
    a["16"] = 4
    a["26"] = 5
    a["28"] = 6
     
    a.sortedKeys = {"1","2","3","16","26","28"}
    In a lot of cases, I think adding the ID would be easier in case you need to sort on other parameters whenever you want.
  • MellsMells Guru
    edited January 2013
    @zvardin
    I have some troubles to see how I could apply it to a calendar example, access easily to a specific day without having to find the corresponding id in the values instead of having access through a numerical key.
    Would you have a very simple example to share for the following?
    calendar[2013][01][24]["status"] = 1
    calendar[2013][01][24]["cat"] = 2
     
    -- easy access
    print (calendar[2013][01][24]["cat"])
    @twisttap
    maybe you can try sqlite3 ?
    I haven't tried sqlite3 yet, I thought storing my datas in a lua table would be enough for my needs.

    thank you
    twitter@TheWindApps Artful applications : The Wind Forest. #art #japan #apps
  • zvardinzvardin Member
    edited January 2013
    I'm not sure I quite get what you're trying to do with the calendar for when you need to loop through it, but you could create a separate table to pair id's while leaving your calendar table intact like so:
        --assume your calendar table code is here
     
        sortedDays = {calendar[2013][01][24], calendar[2013][01][25]}
     
       -- this way when you need to access them in order, you can loop through sortedDays
       for i = 1, #sortedDays do
         sortedDays[i] -- do whatever
       end
     
       -- or still access via your calendar table
       calendar[2013][01][24] -- do something
    If you're not storing specific things for the year and month parts of the table though, it might be easier to change the keys to strings "20130124", etc.

    This way you could also sort the sortedDays table as you wish also. For example:
    -- if you wanted to return a list of days in order of a certain status etc for instance
    table.sort(sortedDays, function(a,b)
       return a.status < b.status
    end)
Sign In or Register to comment.