cj1098
cj1098

Reputation: 1600

sorting a table in lua based on an inner tables value

So currently I have a table in Lua that contains another table (much like a hashtable). It's called email_table, and I have my person_table inside it. The email_table's keys are email_addresses and the person_table holds all information about a person.

Currently what I'm trying to do is sort my email_table based on a value that's inside of person_table. The built in sort function for Lua does not work with such values unfortunately. How would I get started?

Upvotes: 0

Views: 557

Answers (1)

Nicol Bolas
Nicol Bolas

Reputation: 473262

You cannot sort something that isn't an array. If your keys aren't monotonically increasing integers, then you can't sort it. Sorting implies order, and there is no ordering on non-integer keys of tables.

If "The email_table's keys are email_addresses", then email_table cannot be sorted. You can have another table that is a sorted list of email addresses. But this must be a list: the keys must be monotonically increasing integer values (1, 2, 3, 4, etc). Those have an explicit order.

Upvotes: 2

Related Questions