Can you sort tables?

Discuss any general programming issues here
Post Reply
Bugala
Posts: 1400
Joined: Sun Feb 14, 2010 7:11 pm

Can you sort tables?

Post by Bugala »

In my program I have as example this:

Code: Select all

storage[brand$][numbers1$][numbers2$][numbers3$] = { ["2.5"] = {}, ["3"] = {}, ["3.5"] = {}, ["4"] = {}, ["4.5"] = {}, ["5"] = {},
								   ["5.5"] = {}, ["6"] = {}, ["6.5"] = {}, ["7"] = {}, ["7.5"] = {}, ["8"] = {}, ["8.5"] = {}, 
								   ["9"] = {}, ["9.5"] = {} }
at later point there comes:

Code: Select all

ForEach(storage[brand$][numbers1$][numbers2$][numbers3$], p_CreateBoxInputButton)


Function p_CreateBoxInputButton(key, square)
TextOut(textx, texty, key, {name="text"..buttonnumber})

Naturally there is much more code here, but to put it short, idea is that it wil list all those numbers and purpose would be that it would list them in order of:

2.5
3
3.5
4
4.5...


However, now when i use that foreach function to go through all these numbers, what i in practice get is all the numbers in same order each time, but in wrong order.
They go in way of:
4.5
3.5
6.5
2
5
4
7...


I tried to look from manual, but i couldnt quite figure out wether it is possible to sort tables or not.

For notice that these numbers are not actually numbers, but strings, and not just strings, but tables that are named as strings.


So is there anyway that i can sort these tables so that when i use "ForEach" command, it would bring them out in order of 2.5, 3, 3.5, 4...
or do i need to rewrite my program (which is possible, but this had just been better way to do it)
Bugala
Posts: 1400
Joined: Sun Feb 14, 2010 7:11 pm

Re: Can you sort tables?

Post by Bugala »

How does "Foreach" command decide the order of execution of the list?

I have tried couple of different ways for my program with results i didnt quite expect.


Instead of using ["2"] i made the table with [2].

When i used [2] = {}, [3] = {}, [4] = {}...

It resulted in foreach executing them in order of 2, 3, 4... as i expected.

However, my problem is that i am trying to store different sized shoes, and shoe sizes include half sizes, ie. 2.5, 3.5, 4.5.

So i tried using [2] = {}, [2.5] = {}, [3] = {}, [3.5] = {}...


When i now executed foreach command, it didnt work out so well anymore. It executed the 2, 3, 4... and so on in that order, but 2.5, 3.5... and so on, it executed in completely different order. Order is everytime the same, but is is not 2.5, 3.5, 4.5, but in different order.


At this point i reverted to my plan C (previous was Plan B already), in which i used [20] = {}, [25] = {}, [30] = {}... and thought this would solve my problem, but i was wrong, this one resulted in something unexpected. For whole list was out of order. it didnt come as [20], [25], [30] as i had expected, but the order was something else than orderly order.


So Andreas, how does "ForEach" command determine in which order it processes through the tables cells, and is there any way to affect the order?
User avatar
airsoftsoftwair
Posts: 5887
Joined: Fri Feb 12, 2010 2:33 pm
Location: Germany
Contact:

Re: Can you sort tables?

Post by airsoftsoftwair »

So Andreas, how does "ForEach" command determine in which order it processes through the tables cells, and is there any way to affect the order?
I don't really know :) This is all about how lua internally stores arrays. Maybe check in a lua forum about this. Hollywood's ForEach instruction is just the same as the lua equivalent. From my mind, I suppose an order like this: First table elements 1,2,3...n, AFTER that follows table element 0 (because lua tables normally start at 1; Hollywood uses a hack here), AFTER that non integer indices like 1.5, "foobar", etc. But I don't know if there is any order in how these non integer indices are handled. Ask some lua experts :)
Bugala
Posts: 1400
Joined: Sun Feb 14, 2010 7:11 pm

Re: Can you sort tables?

Post by Bugala »

Okay, i guess i will ask.

I might at some point put some example results here on different ways i used to create the table just for the interest, but I would have one feature request concerning foreach command.

Thing is, I found a way to sort of sort them, but there comes minor issue with it (the 0 issue).

What i did was to use foreach command to go through the table ie. numbers1$

Then I collected all the KEY names (ie. number1$["2.5"] in a new table using:

Code: Select all

temptable = {}
InsertItem(templist, key)
After this templist was completed, i used

Code: Select all

Sort(templist)
This resulted in me having all the number in right order in table cells [0], [1], [2]...

When i after this used ForEach command, I finally got myself a sorted list that had the shoe sizes go from 2.5 to 9.5 in order, Except that the first (2.5) size being in cell [0], resulted in this cell being executed as last.


Therefore my feature request would be to make Sort command compatible with ForEach command in that listing sorted list using foreach command would result in list made in right order instead of first one falling to last place.


Its not a bit deal in sense that you can quite easily get around it even several ways i guess, but it would be less hassle anyway.
Post Reply