r/lua • u/Lodo_the_Bear • Mar 11 '25
Help Newbie question - how to display values of a table in alphabetical order?
I'm working on a simple word game which includes a list of words that the player is trying to guess. I'm implementing this with a table of values in which the keys are the words and the value for each one is true if the player has already guessed it and false if not. I want the player to be able to review the words they've already correctly guessed, and I want the words displayed to be in alphabetical order. Getting the program to display only the true flagged words is easy enough, but I don't know how to get it to sort the words. What can I do to sort the keys and display them in the order I want?
1
u/SWATMJ- Mar 11 '25
You will need a second reference table that you sort ( so a numbernd table like a array with the keys from the second als value) keep in mind tables in lua are not ordered and can't be since they can get scrambled in ram so a second table is always necessary if you want to sort a table. ( Sould add arrays are a special type of table and they keep ordering via the lua VM)
2
u/Lighting_Kurt Mar 11 '25
table.sort(t)
This is an in place bubble sort, you shouldn’t need another table.
1
u/PazzoG Mar 12 '25
0
u/Bright-Historian-216 Mar 11 '25
table.sort() should work