php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #37480 array_multisort SORT_STRING not working as a MySQL SELECT with ORDER BY would
Submitted: 2006-05-17 16:57 UTC Modified: 2006-05-17 21:39 UTC
From: 8umucsxy at terra dot es Assigned:
Status: Not a bug Package: Feature/Change Request
PHP Version: 4.4.2 OS:
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: 8umucsxy at terra dot es
New email:
PHP Version: OS:

 

 [2006-05-17 16:57 UTC] 8umucsxy at terra dot es
Description:
------------
There is a problem with array_multisort in languages other than English.
For special chars, as A with accent (?), the sorting does not correspond to what might expect from a MySQL SELECT with ORDER BY.
 
For example the code attached to the message
  
will sort the array in this way: ABADIA, ALVAREZ, BU?UEL, ZUBIETA, ?LVARES
 
while a MySQL SELECT with ORDER BY nombre ASC will yield
 
ABADIA, ?LVARES, ALVEREZ, BU?UEL, ZUBIETA
 
as A and ? are considered two different representations of the same letter.


Reproduce code:
---------------
 foreach ($students as $key => $row){
          $surname[$key] = $row['surname'];
       }
       array_multisort($surname, SORT_ASC, $students);


Expected result:
----------------
ABADIA, ?LVARES, ALVEREZ, BU?UEL, ZUBIETA

Actual result:
--------------
ABADIA, ALVAREZ, BU?UEL, ZUBIETA, ?LVARES

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2006-05-17 21:30 UTC] derick@php.net
How can you have a feature request with reproducable code? And this is all already very possible:

<?php
setlocale(LC_ALL, 'es_ES');

$words = array( ABADIA, ALVAREZ, BU?UEL, ZUBIETA, ?LVARES );

sort( $words, SORT_LOCALE_STRING);
var_dump($words);
?>

outputs:
array(5) {
  [0]=>
  string(6) "ABADIA"
  [1]=>
  string(7) "?LVARES"
  [2]=>
  string(7) "ALVAREZ"
  [3]=>
  string(6) "BU?UEL"
  [4]=>
  string(7) "ZUBIETA"
}

 [2006-05-17 21:39 UTC] 8umucsxy at terra dot es
I include the code at the end of my comment.
Yes, that sorting you provide is possible and indeed more convenient, but notice that this is just an example. I used array_multisort for a real-life example where I needed to order by different keys, one numeric, the other two strings, and I found the problem. Indeed the only shortcut I was able to get was to temporaly disable the accent character by including another key with the letter converted to the un-accented equivalen, i.e. "?" => "A".

I believe the reason is that array_multisort uses A-Z,...?..., etc., to order, so including the charset as a parameter in a next version could be interesting.
 [2006-05-17 21:39 UTC] 8umucsxy at terra dot es
<?php
		require_once("includes/conn.php");

		$sql = "SELECT * FROM apellidos.apellidos ORDER BY apellido";
		$res = mysql_query($sql);
		while($row=mysql_fetch_object($res)){
			echo $row->apellido . ",";
		} // while

		echo "<br>";

	   $students = array("BU?UEL", "ABADIA", "?LVARES", "ZUBIETA", "ALVAREZ");
	   foreach ($students as $key => $row){
         $surname[$key] = $row['surname'];
       }
       array_multisort($surname, SORT_ASC, $students);

		for($i=0;$i<count($students);$i++){
			echo $students[$i] . ", ";
		} // for


?>
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Sun Jul 13 15:01:33 2025 UTC