php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #28527 Sorting arrays in utf-8 charset not possible
Submitted: 2004-05-26 14:06 UTC Modified: 2004-05-26 17:40 UTC
From: marek at lewczuk dot com Assigned:
Status: Not a bug Package: Arrays related
PHP Version: 5CVS-2004-05-26 (dev) OS: Windows XP
Private report: No CVE-ID: None
 [2004-05-26 14:06 UTC] marek at lewczuk dot com
Description:
------------
One year ago I have send info about a bug related on sorting arrays with special, language specific characters like ???. The bug has "closed" status, so it should be working. But is not... All characters are encoded in UTF-8 and also I have made proper setLocale. I don't know if this is a problem with Windows or PHP, but I think that this bug will be also present  on Linux machines.

The code:
SetLocale(LC_COLLATE, $GLOBALS['__OS__'] == "WINDOWS" ? "Polish_Poland.65001" : "pl_PL.UTF-8");
SetLocale(LC_CTYPE, $GLOBALS['__OS__'] == "WINDOWS" ? "Polish_Poland.65001" : "pl_PL.UTF-8");
$array = array ("?liwka", "Zook", "Alfa", "Beta", "Shit");
sort($array);
print_r($array);


Current result of sorting:
Array
(
    [0] => Alfa
    [1] => Beta
    [2] => Shit
    [3] => Zook
    [4] => ?liwka
)

And it should be:
Array
(
    [0] => Alfa
    [1] => Beta
    [2] => Shit
    [3] => ?liwka
    [4] => Zook
)


Reproduce code:
---------------
SetLocale(LC_COLLATE, $GLOBALS['__OS__'] == "WINDOWS" ? "Polish_Poland.65001" : "pl_PL.UTF-8");
SetLocale(LC_CTYPE, $GLOBALS['__OS__'] == "WINDOWS" ? "Polish_Poland.65001" : "pl_PL.UTF-8");
$array = array ("?liwka", "Zook", "Alfa", "Beta", "Shit");
sort($array);
print_r($array);

Expected result:
----------------
Array
(
    [0] => Alfa
    [1] => Beta
    [2] => Shit
    [3] => ?liwka
    [4] => Zook
)


Actual result:
--------------
Array
(
    [0] => Alfa
    [1] => Beta
    [2] => Shit
    [3] => Zook
    [4] => ?liwka
)

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-05-26 14:21 UTC] derick@php.net
PHP sorts according to ascii, not a charset so this is not a bug. You will need to use usort() and strcoll() to get this to work.

Not a bug -> bogus.
 [2004-05-26 17:04 UTC] marek at lewczuk dot com
Ok, so lets try to do it with usort() and strcoll(). Strcoll() isn't working at all - it always return 2147483647. So
 [2004-05-26 17:07 UTC] marek at lewczuk dot com
Sorry. You are right. The problem was in not-proper setLocal settings. It is working with usort and strcoll. Sorry for "bogus".
 [2004-05-26 17:40 UTC] marek at lewczuk dot com
Sorry again - it is not working. I have created new bug.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 19 10:01:28 2024 UTC