Sorting in Tables

I have an simple code:

local T = {  
 red = 5,   
 green = 5,   
 blue = 5,   
 purple = 5,   
 white = 5  
 }  
  
for k,v in pairs(T) do  
 print(k,v)  
end  
  

Output:

blue 5  
green 5  
white 5  
purple 5  
red 5  

Why is the sorting in the output different? How can I list the table in the same order as I put the fields?

Thank You [import]uid: 181861 topic_id: 33774 reply_id: 333774[/import]

Well, you just can’t, as Lua indices in tables are considered to be a set, and have no proper order.
See here for more details about that behaviour.

Anyway, you can workaround that easily. You can just use custom-made function, as this sortedIteration snippet (which sorts indices in a table over iteration) or certainly what you are looking for , this phpTables snippet. [import]uid: 142361 topic_id: 33774 reply_id: 134308[/import]

Well, you just can’t, as Lua indices in tables are considered to be a set, and have no proper order.
See here for more details about that behaviour.

Anyway, you can workaround that easily. You can just use custom-made function, as this sortedIteration snippet (which sorts indices in a table over iteration) or certainly what you are looking for , this phpTables snippet. [import]uid: 142361 topic_id: 33774 reply_id: 134308[/import]