Folks, I want to sort following nested collection
by string alphabeticaly:
$collection = collect([
["name"=>"maroon"],
["name"=>"zoo"],
["name"=>"ábel"],
["name"=>"élof"]
])->sortBy("name");
I would expect:
1=> "ábel"
2=> "élof"
3=> "maroon"
4=> "zoo"
I got instead:
1=> "maroon"
2=> "zoo"
3=> "ábel"
4=> "élof"
I seen some PHP threads for this, but I am curious if there is any Laravel workaround for this. Thanks.
Here's a Solid way to do it:
$blank = array();
$collection = collect([
["name"=>"maroon"],
["name"=>"zoo"],
["name"=>"ábel"],
["name"=>"élof"]
])->toArray();
$count = count($collection);
for ($x=0; $x < $count; $x++) {
$blank[$x] = $collection[$x]['name'];
}
$collator = collator_create('en_US');
var_export($blank);
collator_sort( $collator, $blank );
var_export( $blank );
dd($blank);
Outputs:
array (
0 => 'maroon',
1 => 'zoo',
2 => 'ábel',
3 => 'élof',
)array (
0 => 'ábel',
1 => 'élof',
2 => 'maroon',
3 => 'zoo',
)
Laravel Pretty Output:
array:4 [
0 => "ábel"
1 => "élof"
2 => "maroon"
3 => "zoo"
]
For personal Reading and reference: http://php.net/manual/en/class.collator.php
Hope this answer helps, sorry for late response =)