How to sort a collection of UTF-8 strings containing non-Latin chars in Laravel 5.3?
Asked Answered
D

3

5

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.

Donnetta answered 7/9, 2016 at 16:13 Comment(0)
S
1

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 =)

Savoy answered 2/11, 2016 at 5:31 Comment(0)
S
8

Well, I had this problem and I was able to solve it like this:

$list = $Company->administrator->sortBy(function($adm){
   return iconv('UTF-8', 'ASCII//TRANSLIT', $adm->person->name);
});

My environment was Laravel 5.5 and PHP 7.1

Shoshanashoshanna answered 22/6, 2018 at 14:53 Comment(1)
In some cases you might want to add an strtolower on the property you are sorting byNeurath
S
4

You don't need to use the Collator class in this case. Laravel's collection sortBy uses asort() and arsort() internally, which has the SORT_LOCALE_STRING flag for sorting according to the currently set locale.

So, your example can be written as follows:

setlocale(LC_COLLATE, 'fr_FR.utf8'); // No need to set this if you're doing it elsewhere

$collection = collect([
    ["name"=>"maroon"],
    ["name"=>"zoo"],
    ["name"=>"ábel"],
    ["name"=>"élof"]
])->sortBy("name", SORT_LOCALE_STRING); // Signals to arsort() to take locale into consideration

This also means you don't have to convert back-and-forth from a generic PHP array to a Laravel collection.

Stereotaxis answered 16/1, 2017 at 16:56 Comment(1)
setlocale() is not threadsafe. See the warning: php.net/manual/en/function.setlocale.phpRetaliate
S
1

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 =)

Savoy answered 2/11, 2016 at 5:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.