|   | php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login | 
| 
  [2009-02-23 13:40 UTC] carsten_sttgt at gmx dot de
 Description:
------------
Hello,
why is the nat_char defined as char instead of unsigned char?
char limit us (and a correct sorting) to ASCII 0-127. With a unsigned char (ASCII 0-255) the sorting is "correct" for all single byte charsets like iso-8850-1 (which is the default in PHP).
Internally the function is already doing a cast to unsigned char many times (but not in the main comparison).
In the original header (strnatcmp.h) from the author, the typedef for nat_char is also only a hint.
Regards,
Carsten
Reproduce code:
---------------
<?php
$daten = array('S?den','spielen','Sonne','Wind','Regen','Meer');
natcasesort($daten);
print_r($daten);
?>
Expected result:
----------------
Array
(
    [5] => Meer
    [4] => Regen
    [2] => Sonne
    [1] => spielen
    [0] => S?den
    [3] => Wind
)
Actual result:
--------------
Array
(
    [5] => Meer
    [4] => Regen
    [0] => S?den
    [2] => Sonne
    [1] => spielen
    [3] => Wind
)
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits             | |||||||||||||||||||||||||||||||||||||
|  Copyright © 2001-2025 The PHP Group All rights reserved. | Last updated: Fri Oct 31 04:00:01 2025 UTC | 
> The strnatcmp uses the zend_parse_paramters function to parse > the function parameters. Ah, ok, I_m not familiar with the PHP/Zend internals (or C...). Just a question about the difference between natsort() and asort(). Should they not work in the same way if you have an array without numbers in the key values? And if I look into array.c, PHP_FUNCTION(asort) is also using zend_parse_parameters. e.g. IMHO this script should result in 2 times the same output: <?php $datensort = $datennat = $daten = array( 'S?den','spielen','Sonne','Wind','Regen','Meer' ); natsort($datennat); print_r($datennat); asort($datensort); print_r($datensort); ?> Regards, Carsten