-
Notifications
You must be signed in to change notification settings - Fork 11.6k
Closed
Closed
Copy link
Labels
Description
Laravel Version
11.40.0
PHP Version
8.3.1
Database Driver & Version
No response
Description
Collections ->sortBy()
multiple attributes that contain Enum return wrong order
sort by single attribute
✅ String
✅ Int
✅ Carbon
✅ Enum
sort by multiple attributes
✅ String
✅ Int
✅ Carbon
❌ Enum
Steps To Reproduce
Enum
<?php
namespace App\Enums;
enum Role: int
{
case Administrator = 1;
case Moderator = 2;
case SuperMember = 3;
case Member = 4;
}
Test in route
<?php
use App\Enums\Role;
use Illuminate\Support\Facades\Route;
Route::get('/test', function () {
$users = collect([
['id' => 1, 'role_id' => Role::Administrator],
['id' => 2, 'role_id' => Role::Moderator],
['id' => 3, 'role_id' => Role::SuperMember],
['id' => 4, 'role_id' => Role::Member],
['id' => 5, 'role_id' => Role::Administrator],
['id' => 6, 'role_id' => Role::Moderator],
['id' => 7, 'role_id' => Role::SuperMember],
['id' => 8, 'role_id' => Role::Member],
]);
$noSorting = $users
->pluck('role_id')
->toJson();
dd($noSorting); // "[1,2,3,4,1,2,3,4]"
$asc = $users
->sortBy([
['role_id', 'asc'],
['id', 'asc'],
])
->pluck('role_id')
->toJson();
$desc = $users
->sortByDesc([
['role_id', 'desc'],
['id', 'desc'],
])
->pluck('role_id')
->toJson();
dd(
'asc' => $asc, // "[4,4,3,3,2,2,1,1]" expecting 1,1,2,2,3,3,4,4
'desc' => $desc, // "[4,2,3,1,4,3,2,1]" expecting 4,4,3,3,2,2,1,1
);
});