0

Given the following array:

$foo = ["a", "B", "æ", "Æ", "c", "A", "b", "C", "1"];

Then I set the locale with setlocale(LC_ALL, ['nb_NO.UTF-8', 'no_NO.UTF-8']); and run the array through sort($foo, SORT_LOCALE_STRING).

In Ubuntu, the sorted array will then look like:

[
  0 => '1',
  1 => 'A',
  2 => 'a',
  3 => 'B',
  4 => 'b',
  5 => 'C',
  6 => 'c',
  7 => 'Æ',
  8 => 'æ',
]

While on Mac (OS X) I get:

[
  0 => '1',
  1 => 'A',
  2 => 'B',
  3 => 'C',
  4 => 'a',
  5 => 'b',
  6 => 'c',
  7 => 'Æ',
  8 => 'æ',
]

It seems OS X wants to sort strings starting with capital letters by themselves(ABC, then abc), while I would just like them to be together (AaBbCc).

Is there any way of having them sort the arrays the same way in PHP, or would I have to write a custom sorting method, using one of the u*sort() methods instead?

Edit: seems to be quite similar to the question this is marked as a duplicate of. Altough OS X still seems to sort upper- and lower case letters after each other instead of mixing them, it is fixable by adding strtolower() to the sorting function.

carestad
  • 3,196
  • 2
  • 13
  • 10
  • So you want code for PHP to sort correctly? Little unclear. – Andreas May 08 '19 at 08:20
  • Possible duplicate of [Sort multidimensional array with special characters in php](https://stackoverflow.com/questions/43070933/sort-multidimensional-array-with-special-characters-in-php) – Andreas May 08 '19 at 08:27
  • @Andreas Sorry! I will definitely not mind example code on how to sort it the same way in PHP, but with custom code. Also won't mind to know if there is something wrong with the behind-the-scenes-algorithm PHP uses in OS X, and if it can be fixed by changing a setting or something in PHP. – carestad May 08 '19 at 08:56
  • @Andreas not quite a duplicate though. The first example with using `iconv` ans `TRANSLIT` it will turn `Æ` into `AE` etc., så they will end up at the beginning of the array, instead of the end where I want them. The other one will also sort upper and lower case letters differently. – carestad May 08 '19 at 09:56

1 Answers1

0

The cleanest, most professional way to fix this issue would be to ensure that you are actually setting an identical locale in both environments. Please read though the manual and the comments presented below the specs for insights about how to properly set and get the locale. Setting the locale can be fiddly business, you may need to change the syntax for some, and declare fallback values for others. The toil is yours to undertake.

As for a hot/temporary fix, you can call usort() with the spaceship operator placed between an array of conditions. First, compare $a vs $b after multibyte-safe converting them both to uppercase, if that comparison is a tie, then the spaceship operator will make a comparison on the raw $a vs $b.

Code: (Demo)

$foo = ["a", "B", "æ", "Æ", "c", "A", "b", "C", "1"];

usort($foo, function($a, $b) {
    return [mb_strtoupper($a), $a] <=> [mb_strtoupper($b), $b];
});

var_export($foo);

Output:

array (
  0 => '1',
  1 => 'A',
  2 => 'a',
  3 => 'B',
  4 => 'b',
  5 => 'C',
  6 => 'c',
  7 => 'Æ',
  8 => 'æ',
)
mickmackusa
  • 43,625
  • 12
  • 83
  • 136
  • @carestad what is the status of your question? Does my answer resolve your issue? Do you need additional support? Please progress your question. – mickmackusa May 15 '19 at 13:15