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

Add table.sort #3

Closed

Conversation

RiskoZoSlovenska
Copy link
Contributor

Behaves identically to the standard function, but also returns the sorted table (can work as a drop-in replacement). I find this functionality immensely useful in my projects.

Behaves identically to the standard function, but also returns the sorted table
@Bilal2453
Copy link
Contributor

May I ask for an example where this is useful? Wouldn't it be that if you are sorting a table, you already have a reference to it?

@RiskoZoSlovenska
Copy link
Contributor Author

Say for example you want to sort a table and then perform some other function on it, like table.slice:

table.sort(scores)
local topThree = table.slice(scores, 1, 3)

-- vs

local topThree = table.slice(table.sort(scores), 1, 3)

Or, for example, if sorting a table is the last step in some function, with this you'd be able to just return table.sort(tbl) instead of having to sort and return it on two separate lines:

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

@RiskoZoSlovenska
Copy link
Contributor Author

Now that I'm thinking about it, it may also be a good idea to have table.shift return the input table as well, for the same reasons as mentioned above. It'll also be more consistent since most of the in-place functions (table.join, table.merge, etc.) already return the input table even though they don't strictly have to.

@truemedian
Copy link
Owner

I do not believe that overriding standard library functions with non-compliant forms is a good idea.

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

Successfully merging this pull request may close these issues.

3 participants