#21728 [Opn]: Additional (kindly weird example) for sort()
| From: | philip@php.net | Date: | Sat, 18 Jan 2003 17:48:57 +0000 |
| Subject: | #21728 [Opn]: Additional (kindly weird example) for sort() | ||
| References: | 1 | Groups: | php.doc |
| Request: | Send a blank email to phpdoc+get-969351014@lists.php.net to get a copy of this message | ||
ID: 21728
Updated by: philip@php.net
Reported By: andrey@php.net
Status: Open
Bug Type: Documentation problem
Operating System: All
-PHP Version: 4.3.0
+PHP Version: 4.4.0-dev
New Comment:
How about:
<?php
$arr1 = array("a","b","4",5,4,"true","TRUE",true,
false, "c");
sort($arr1);
var_dump($arr1);
?>
Which gives:
array(10) {
[0]=>
bool(false)
[1]=>
string(4) "TRUE"
[2]=>
string(1) "a"
[3]=>
string(4) "true"
[4]=>
bool(true)
[5]=>
string(1) "b"
[6]=>
string(1) "c"
[7]=>
int(4)
[8]=>
string(1) "4"
[9]=>
int(5)
}
Which is weird as "4" looks misplaced. For example in this:
<?php
$arr1 = array("a","b","4",5,4,"true","TRUE",true,
false, "c", "d");
sort($arr1);
var_dump($arr1);
?>
We get different results (all I added was "d" to the end):
array(11) {
[0]=>
bool(false)
[1]=>
string(1) "4"
[2]=>
string(4) "TRUE"
[3]=>
string(1) "a"
[4]=>
string(1) "b"
[5]=>
string(1) "c"
[6]=>
string(1) "d"
[7]=>
string(4) "true"
[8]=>
bool(true)
[9]=>
int(4)
[10]=>
int(5)
}
Notice the different order, is this a genuine bug?
Previous Comments:
------------------------------------------------------------------------
[2003-01-18 10:26:27] andrey@php.net
Today I closed bug #21444. The user has to master the type juggling to
know the expected output. I think that it is good idea to add it's
example as comprehensive one.
The script goes here (the explanation is after it) :
<?php
$arr1 =
array("a","b","c","d","4",5,4,"true","TRUE",true);
sort($arr1);
var_dump($arr1);
?>
The output is :
array(10) {
[0]=>
bool(true)
[1]=>
int(4)
[2]=>
string(1) "4"
[3]=>
string(4) "TRUE"
[4]=>
string(1) "a"
[5]=>
string(1) "b"
[6]=>
string(1) "c"
[7]=>
string(1) "d"
[8]=>
string(4) "true"
[9]=>
int(5)
}
It may look strange - why (int)5 is after all the strings. This is
because "4" is lower than (int) 5, "4" is before "true" and
"true" is
before 5. The first 2 are obvious, the third one is not. But it is ok.
It's better not to mix types in the array. If 5 is changed to "5" then
"5" goes right after "4".
Thanks
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=21728&edit=1