Естественный алгоритм сортировки в PHP с поддержкой Unicode?

Вам необходимо переслать объявление tuEntry, чтобы вы могли использовать его в ts_EntrySubmenu. Вам нужно будет дать имя тегу этому объединению, чтобы на него можно было ссылаться позже.

Кроме того, вы не можете инициализировать поле структуры или объединения по умолчанию, когда оно определено в C. Это поле необходимо установить в каждом соответствующем экземпляре.

typedef union tuEntry tuEntry;

typedef struct {
  char* entryName;
  const te_UIEntryType entryType;   // no default value
  tuEntry *entries[];
} ts_EntrySubmenu;


union tuEntry
{
  tsEntry entry;
  ts_EntryInt entryInt;
  ts_EntrySingle entrySingle;
  ts_EntryBool entryBool;
  ts_EntryDiscrete entryDiscrete;
  ts_EntrySubmenu entrySubmenu;
};
20
задан hippietrail 19 April 2011 в 09:08
поделиться

2 ответа

The question is not as easy to answer as it seems on the first look. This is one of the areas where PHP's lack of unicode supports hits you with full strength.

Frist of all natsort() as suggested by other posters has nothing to do with sorting arrays of the type you want to sort. What you're looking for is a locale aware sorting mechanism as sorting strings with extended characters is always a question of the used language. Let's take German for example: A and Ä can sometimes be sorted as if they were the same letter (DIN 5007/1), and sometimes Ä can be sorted as it was in fact "AE" (DIN 5007/2). In Swedish, in contrast, Ä comes at the end of the alphabet.

If you don't use Windows, you're lucky as PHP provides some functions to exactly this. Using a combination of setlocale(), usort(), strcoll() and the correct UTF-8 locale for your language, you get something like this:

$array = array('Àgile', 'Ágile', 'Âgile', 'Ãgile', 'Ägile', 'Agile', 'Test');
$oldLocal = setlocale(LC_COLLATE, '<<your_RFC1766_language_code>>.utf8');
usort($array, 'strcoll');
setlocale(LC_COLLATE, $oldLocal);

Please note that it's mandatory to use the UTF-8 locale variant in order to sort UTF-8 strings. I reset the locale in the example above to its original value as setting a locale using setlocale() can introduce side-effects in other running PHP script - please see PHP manual for more details.

When you do use a Windows machine, there is currently no solution to this problem and there won't be any before PHP 6 I assume. Please see my own question on SO targeting this specific problem.

25
ответ дан 30 November 2019 в 00:01
поделиться
natsort($array);
$array = array_values($array);
1
ответ дан 30 November 2019 в 00:01
поделиться
Другие вопросы по тегам:

Похожие вопросы: