php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #38635 sort and it's similar commands discriminate agaisnt case.
Submitted: 2006-08-29 00:36 UTC Modified: 2006-08-29 19:16 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: xconspirisist at gmail dot com Assigned:
Status: Not a bug Package: Documentation problem
PHP Version: Irrelevant OS: Linux
Private report: No CVE-ID: None
 [2006-08-29 00:36 UTC] xconspirisist at gmail dot com
Description:
------------
I assume this is a documentation problem, as I hardly think that this could possibly be the intended output.

If you use lower case letters at the beginning of strings that are part of an array, and then use the sort() function on the array, the sort() function will take preference over upper case letters.

Reproduce code:
---------------
<?php

$a = array('apples', 'Banannas', 'Carrots' );

sort($a);

echo implode($a, ', ');

?>

Expected result:
----------------
apples, Banannas, Carrots

Actual result:
--------------
Banannas, Carrots, apples

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2006-08-29 06:18 UTC] hholzgra@php.net
This is definetly the intended resut as all PHP string comparisons are case sensitive (using strcmp() for comparison)
and as the lower case letters have higher character codes than
the lower case ones (in all common character sets like ASCII, iso-8859-x, Unicode)

For case insensitive sorting you need to explicitly specify the comparison function in a user defined sort:

  <?php
  $a = array('apples', 'Banannas', 'Carrots' );
  usort($a, "strcasecmp");
  echo implode($a, ', ');
  ?>
 [2006-08-29 19:16 UTC] xconspirisist at gmail dot com
Okay. How about adding this suggestion to the manual though? It might clarify things for others too.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Sun Jul 20 10:00:03 2025 UTC