Sorting through JSON tables in order

Hey guys,

I am using a JSON list which gets decoded into a lua table in my code.  I am using that data to build a list of text objects. (Very similar to what you would use a Table view for)

All works well except that the order of the text items from my JSON list is not in the same order that it is in the original JSON string.  It doesn’t seem to randomize each time I run my app, but its just never in order.  Can anyone give me an idea of why this is happening and how I can get my JSON string to keep its order?

This is the code I am using to cycle through my decoded JSON string to get the data I need:

for key,value in pairs(featureTable) do      //Build a text object here end  

It’s because key indexes which are not numbers, in json object and in lua tables have no order and it changes and can differ. To have order, you must have table with numeric indexes for example

[lua]
local words = {‘apple’, ‘orange’, ‘pear’}
[/lua]

It always will be in order and you iterate through it using ipairs (keys will be 1, 2, 3 …) or [i] where i is index from 1 to size of table (# signs return size of table but only with indexed tables and this nethod is faster then ipairs)

If you write
[lua]
Local words = { fruit1 = ‘apple’, fruit2 = ‘orange’, fruit3 = ‘pear’ }
[/lua]

Then order is as application thinks is best so it’s not as you declared.

There is a table.sort() function you can use to order the data. 

Even dictionary type tables? I think it only works on number indexed tables like in plain C or did I really miss something?

It’s because key indexes which are not numbers, in json object and in lua tables have no order and it changes and can differ. To have order, you must have table with numeric indexes for example

[lua]
local words = {‘apple’, ‘orange’, ‘pear’}
[/lua]

It always will be in order and you iterate through it using ipairs (keys will be 1, 2, 3 …) or [i] where i is index from 1 to size of table (# signs return size of table but only with indexed tables and this nethod is faster then ipairs)

If you write
[lua]
Local words = { fruit1 = ‘apple’, fruit2 = ‘orange’, fruit3 = ‘pear’ }
[/lua]

Then order is as application thinks is best so it’s not as you declared.

There is a table.sort() function you can use to order the data. 

Even dictionary type tables? I think it only works on number indexed tables like in plain C or did I really miss something?