How can I sort this?

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec in rhoncus nulla, blandit accumsan velit. Pellentesque rhoncus placerat magna, eu suscipit nisi ornare finibus. Sed ante orci, mattis nec tempor vitae, faucibus sed nisi. Nullam vel ex augue. Integer eget feugiat tortor, sed vehicula lorem. Sed interdum venenatis elit. Ut erat metus, dictum ac blandit vel, porta non magna. Ut malesuada euismod nibh, nec blandit elit interdum in. Phasellus nec feugiat velit. Integer non turpis euismod, egestas ligula vitae, ornare ligula. Donec non nisl dignissim, dictum enim vitae, imperdiet ligula.

The problem is that this isn’t an array. You’ve inadvertently created a hashmap, which has no particular order, so you can’t sort the entries.

The only way to get around this is to create an array of objects and then sort it using a custom sort function, like so:

my_table = {
	{ Name = "John"; Value = 5; },
	{ Name = "Abby"; Value = 7; },
	{ Name = "Nick"; Value = 2; }
}

table.sort(my_table, function(a, b)
	return a.Value < b.Value
end)

Hopefully this points you in the right direction!

1 Like