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
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
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

Add a Patch

Pull Requests

Add a Pull Request

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-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 19 21:01:30 2024 UTC