I’ve got a table array of values that I’d like to have ordered based on a numerical value, however - the first character in each value is a letter (which I’d like to ignore.)
For example:
[lua]balls = { “b42”, “i11”, “f23” }[/lua]
I am trying to sort solely by the numbers following the letter, but still return a table that includes said letters, i.e.
[lua]sortedballs = { “i11”, “f23”, “b42” }[/lua]
Thanks to some earlier help I can grab the numbers as substrings easily enough, then use table.sort(test, function(a,b) return tonumber(a) < tonumber(b) end) to sort them - but at that point I’ve lost the letters.
I can’t split it into key/values and sort that, because both the number and/or letter can occur more than once, in which case the key is overwritten using it either way. (there could be “c12” and “d12” which prohibits me from using 12 as a key value, or “c12” and “c15” which would overwrite “c” as an example)
It might be wishful thinking, but is there a way to pass in a function to:
[lua]table.sort(test, function(a,b) return tonumber(a) < tonumber(b) end)[/lua]
so that the sort function compares (a) and (b) as something like:
[lua]string.sub(balls[i], 2)[/lua]
I’ve tried all sorts of newbie things at this point, but I’m still unable to get the desired result (and at this rate I’ll end up with a page full of garbage code to make it happen… if I’m lucky :P) so any pointers are much appreciated if it does indeed need to be torn apart and re-assembled somehow.
Thanks!
[import]uid: 49694 topic_id: 21029 reply_id: 321029[/import]