Skip to content

【10.x】Individual wrapping in Collection::sortByMany allows sorting with multiple attributes by just an array of attributes(without direction, defaulting to asceding sort), but the spec is missing in docs #10581

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

Merged
merged 2 commits into from
Jul 7, 2025

Conversation

enes1004
Copy link

@enes1004 enes1004 commented Jul 7, 2025

Calling sortBy with a flat array allows sorting asc by multiple attributes.

The method works but not documented, causing confusion by explicit directions or workarounds like chaining sortBy in reverse order, i.e. $collection->sortBy('second-priority')->sortBy('first-priority'), which does not happen in similir Builder::orderBy method.

Caution

I haven't tested this on versions above 11 or below 9, so, not confident if it works for other versions

Example

> collect([['order_index'=>3,'id'=>4],['order_index'=>2,'id'=>1],['order_index'=>1,'id'=>3],['order_index'=>1,'id'=>2]])->sortBy(['order_index','id'])
= Illuminate\Support\Collection {#7016
    all: [
      3 => [
        "order_index" => 1,
        "id" => 2,
      ],
      2 => [
        "order_index" => 1,
        "id" => 3,
      ],
      1 => [
        "order_index" => 2,
        "id" => 1,
      ],
      0 => [
        "order_index" => 3,
        "id" => 4,
      ],
    ],
  }

Why it works?

In Collection::sortByMany, When passing each comparisons,

  • Arr::wrap is called, which wraps string to array
  • 1st index of array would be undefined, but it defaults to true for $ascending

https://github.com/laravel/framework/blob/37455bbd9ece2ab48443b4ad2af85abf2140e326/src/Illuminate/Collections/Collection.php#L1458-L1460

@enes1004 enes1004 changed the title 【FIX】Individual wrapping in Collection::sortByMany allows sorting with multiple attributes are possible by just an array, missing in docs ( defaulting to ascending sort for each) 【10.x】Individual wrapping in Collection::sortByMany allows sorting with multiple attributes are possible by just an array, missing in docs ( defaulting to ascending sort for each) Jul 7, 2025
@enes1004 enes1004 changed the title 【10.x】Individual wrapping in Collection::sortByMany allows sorting with multiple attributes are possible by just an array, missing in docs ( defaulting to ascending sort for each) 【10.x】Individual wrapping in Collection::sortByMany allows sorting with multiple attributes by just an array, missing in docs ( defaulting to ascending sort for each) Jul 7, 2025
@enes1004 enes1004 changed the title 【10.x】Individual wrapping in Collection::sortByMany allows sorting with multiple attributes by just an array, missing in docs ( defaulting to ascending sort for each) 【10.x】Individual wrapping in Collection::sortByMany allows sorting with multiple attributes by just an array of attributes(without direction, defaulting to asceding sort), but the spec is missing in docs Jul 7, 2025
@taylorotwell taylorotwell merged commit fdf1d85 into laravel:10.x Jul 7, 2025
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.

2 participants