php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #19850 sorting a String case-sensitive fails
Submitted: 2002-10-10 10:41 UTC Modified: 2002-11-17 20:15 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: akorthaus at web dot de Assigned:
Status: Not a bug Package: Arrays related
PHP Version: 4CVS-2002-10-10 OS: Linux + Windows
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
MUST BE VALID
Solve the problem:
5 + 47 = ?
Subscribe to this entry?

 
 [2002-10-10 10:41 UTC] akorthaus at web dot de
I tried the following script to sort an Array case-sensitive:


<?php
$test = array("B",
              "a",
              "b",
              "A",
              "BB",
              "AA");
usort($test, 'strcasecmp');
print_r($test);
?>

What I expected to get was:

Array
(
    [0] => A
    [1] => a
    [2] => AA
    [3] => B
    [4] => b
    [5] => BB
)

What I got on Win2000/PHP 4.2.4-dev was:

Array
(
    [0] => A
    [1] => a
    [2] => AA
    [3] => b
    [4] => B
    [5] => BB
)

and using Linux with PHP 4.1.2 I got

Array
(
    [0] => a
    [1] => A
    [2] => AA
    [3] => B
    [4] => b
    [5] => BB
)

both is not correct. 


regards 

Andreas

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2002-10-27 19:56 UTC] sterling@php.net
it works for me:

Array
(
[0] => A
[1] => a
[2] => AA
[3] => b
[4] => B
[5] => BB
)

Which is exactly what you should get... and the algorithm is platform independent in CVS....
 [2002-11-17 07:01 UTC] akorthaus at web dot de
Sorry that I open it again, but what should be the sense of sorting an array like

Array
(
[0] => A
[1] => a
[2] => AA
[3] => b
[4] => B
[5] => BB
)

usort($test, 'strcasecmp'); 

should sort case-sensitive, that means a,A, b,B, c,C... but what is A,a, b,B??? There is no logical algorithm to sort this way. What is the next? c,C or C,c? How can I get a,A, b,B, c,C..., means _real_ case-sensitive?
 [2002-11-17 11:09 UTC] michael dot mauch at gmx dot de
You want to use natcasesort().

strcasecmp() compares strings case-_in_sensitively - that means that 
'A' =='a', so your usage of usort() does exactly what it should.
 [2002-11-17 20:15 UTC] sniper@php.net
See previous comment.

 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 25 19:01:33 2024 UTC