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

Allow sorting of route:list by multiple column/factors using a comma #50998

Merged
merged 6 commits into from
Apr 10, 2024

Conversation

fredbradley
Copy link
Contributor

@fredbradley fredbradley commented Apr 10, 2024

Today I had need to run artisan route:list --sort={SORT} but wanted to not only sort by domain but then by a second factor.

This PR allows users to use the syntax:

artisan route:list --sort=domain,uri

Which first sorts the list by domain column and then by the uri column.

@fredbradley fredbradley changed the title Allow sorting of route:list by multiple column/factors using a column Allow sorting of route:list by multiple column/factors using a comma Apr 10, 2024
@fredbradley
Copy link
Contributor Author

(Ok - I've tried to work out what @StyleCIBot doesn't like, but can't figure it out from their diff).

@taylorotwell taylorotwell merged commit 8929ee9 into laravel:11.x Apr 10, 2024
28 checks passed
@medilies
Copy link
Contributor

I guess you can directly explode without checking whether the string contains ,.

@fredbradley
Copy link
Contributor Author

fredbradley commented Apr 12, 2024

@medilies yes, I guess you're right. We could possibly launch into a discussion about computed cost of using explode when you don't need to and gather some xdebug results that benchmark the cost of doing the if statement first, vs just diving into explode when you don't need to.

If I'm honest, that's not the comment I was expecting to get about this. I thought someone was going to comment about the lack of trim - what if someone writes --sort="domain, uri"? We should be trimming the space after the explode just in case.

Two small edits, that someone can PR - would be a great easy first PR too!

@medilies
Copy link
Contributor

@fredbradley I wasn't worried about the micro-computational cost. I just prefer to see compact code like:

protected function sortRoutes($sort, array $routes)
    {
        return collect($routes)->sortBy(explode(',', $sort))->toArray();
    }

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