Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

table: A sort function which returns the input table #12

Open
RiskoZoSlovenska opened this issue Oct 6, 2022 · 1 comment
Open

table: A sort function which returns the input table #12

RiskoZoSlovenska opened this issue Oct 6, 2022 · 1 comment

Comments

@RiskoZoSlovenska
Copy link
Contributor

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:

local function sortedKeys(tbl)
    local keys = table.keys(tbl)
    table.sort(keys)
    return keys
end

-- vs

local function sortedKeys(tbl)
    return table.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.

@ADmcdon
Copy link

ADmcdon commented Oct 6, 2022

Maybe table.modified

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants