Sort table with string keys
Hey, so I've been trying to sort a table. The table contains various item information and looks like this.
Code:
ItemTable = { Code:
local sorted_table = {} I spent some time googling and I'm still not sure if its even possible when using strings for an index but I hope it is, because I would like the table to look that way. Thanks! |
Lua Code:
|
That was quick! Thank you so much.
I really need to learn about pairs and ipairs :D |
Tables with string keys are unordered but you can use pairs to return the key/values in whatever order they're found in.
ipairs is for a numerically orderd tables, the same as Lua Code:
Lua Code:
|
To reiterate and clarify on what Fizzlemizz said, tables with strings as keys are stored in a random order. When you use pairs() to print the key,value pairs in a certain order, you're not actually sorting the table, it's just scanning through and reading/printing them in whatever order you define.
|
It was an answer to a second question, which was to explain what ipairs() and pairs() did. The original question was different than what the title suggested, which was actually how to sort a table of tables.
|
All times are GMT -6. The time now is 02:32 AM. |
vBulletin © 2024, Jelsoft Enterprises Ltd
© 2004 - 2022 MMOUI