You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The standard table.sort() sorts a table in-place and doesn't return anything, which is an annoyance whenever the table needs to also be modified in some way either prior to or after sorting. For example, compare the following:
localfunctionsortedKeys(tbl)
localkeys=table.keys(tbl)
table.sort(keys)
returnkeysend-- vslocalfunctionsortedKeys(tbl)
returntable.sort(table.keys(tbl))
end
Not only is the second snippet considerably shorter, but it is also slightly easier to read. I come across these kinds of use cases fairly often and so I think it's a worthwhile issue to address.
Overriding table.sort isn't a good idea, but I'm lost on as to what to name a function that would have the same behaviour as the stock one but returns the sorted table. table.sort2 could work may be confusing, and table.sorted implies a copy.
The text was updated successfully, but these errors were encountered:
The standard
table.sort()
sorts a table in-place and doesn't return anything, which is an annoyance whenever the table needs to also be modified in some way either prior to or after sorting. For example, compare the following:Not only is the second snippet considerably shorter, but it is also slightly easier to read. I come across these kinds of use cases fairly often and so I think it's a worthwhile issue to address.
Overriding
table.sort
isn't a good idea, but I'm lost on as to what to name a function that would have the same behaviour as the stock one but returns the sorted table.table.sort2
could work may be confusing, andtable.sorted
implies a copy.The text was updated successfully, but these errors were encountered: