note 106900 added to function.sort
| From: | WalterTross at osu1 dot php dot net | Date: | Fri, 16 Dec 2011 22:38:21 +0000 |
| Subject: | note 106900 added to function.sort | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-185102@lists.php.net to get a copy of this message | ||
unless you specify the second argument, "regular" comparisons will be used. I quote from
the page on comparison operators:
"If you compare a number with a string or the comparison involves numerical strings, then each
string is converted to a number and the comparison performed numerically."
What this means is that "10" < "1a", and "1a" < "2",
but "10" > "2". In other words, regular PHP string comparisons are not
transitive.
This implies that the output of sort() can in rare cases depend on the order of the input array:
<?php
function echo_sorted($a)
{
echo "{$a[0]} {$a[1]} {$a[2]}";
sort($a);
echo " => {$a[0]} {$a[1]} {$a[2]}\n";
}
// on PHP 5.2.6:
echo_sorted(array( "10", "1a", "2")); // => 10 1a 2
echo_sorted(array( "10", "2", "1a")); // => 1a 2 10
echo_sorted(array( "1a", "10", "2")); // => 2 10 1a
echo_sorted(array( "1a", "2", "10")); // => 1a 2 10
echo_sorted(array( "2", "10", "1a")); // => 2 10 1a
echo_sorted(array( "2", "1a", "10")); // => 10 1a 2
?>
----
Server IP: 81.88.49.17
Probable Submitter: 93.41.205.102
----
Manual Page -- http://www.php.net/manual/en/function.sort.php
Edit -- https://master.php.net/note/edit/106900
Del: integrated -- https://master.php.net/note/delete/106900/integrated
Del: useless -- https://master.php.net/note/delete/106900/useless
Del: bad code -- https://master.php.net/note/delete/106900/bad+code
Del: spam -- https://master.php.net/note/delete/106900/spam
Del: non-english -- https://master.php.net/note/delete/106900/non-english
Del: in docs -- https://master.php.net/note/delete/106900/in+docs
Del: other reasons-- https://master.php.net/note/delete/106900
Reject -- https://master.php.net/note/reject/106900
Search -- https://master.php.net/manage/user-notes.php