I am trying to do a multiple sortBy on a collection and seems like is not working properly:
$myCollection = collect([
['foo' => 3, 'bar' => null, 'active' => 1],
['foo' => 2, 'bar' => null, 'active' => 1],
['foo' => 1, 'bar' => 1, 'active' => 1],
])->sortBy('foo')->sortBy('bar')->sortBy('active');
Result:
Illuminate\Support\Collection {#417 ▼
#items: array:3 [▼
0 => array:3 [▼
"foo" => 3
"bar" => null
"active" => 1
]
1 => array:3 [▼
"foo" => 2
"bar" => null
"active" => 1
]
2 => array:3 [▼
"foo" => 1
"bar" => 1
"active" => 1
]
]
}
First sorts properly by active (they are all the same = 1)
Then sorts properly by "bar" (null < 1)
Then sortBy('foo') fails, because (2<3), but shows 3 first before 2... Expecting result:
Illuminate\Support\Collection {#417 ▼
#items: array:3 [▼
0 => array:3 [▼
"foo" => 2
"bar" => null
"active" => 1
]
1 => array:3 [▼
"foo" => 3
"bar" => null
"active" => 1
]
2 => array:3 [▼
"foo" => 1
"bar" => 1
"active" => 1
]
]
}
This is a sample I did for the presentation. In my real scenario I am using Collection::macro with custom function callbacks to compare dates... but even in this simple example things are looks like not working.