Doc #69331 [Opn]: Default sort of numbers-as-a-string is not SORT_REGULAR
| From: | cmb@php.net | Date: | Mon, 30 Mar 2015 21:09:21 +0000 |
| Subject: | Doc #69331 [Opn]: Default sort of numbers-as-a-string is not SORT_REGULAR | ||
| References: | 1 | Groups: | php.doc.bugs |
| Request: | Send a blank email to doc-bugs+get-12065@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=69331&edit=1
ID: 69331
Updated by: cmb@php.net
Reported by: greevex at gmail dot com
Summary: Default sort of numbers-as-a-string is not
SORT_REGULAR
Status: Open
Type: Documentation Problem
Package: Documentation problem
Operating System: CentOS 6.6
PHP Version: 5.5.23
Block user comment: N
Private report: N
New Comment:
The default flag is SORT_REGULAR[1], but the description "(don't
change types)" is misleading. Actually, SORT_REGULAR works like the
relational (aka. comparison) operators[2], so, for instance,
numeric strings are sorted as numbers.
[1] <http://lxr.php.net/xref/PHP_5_6/ext/standard/array.c#165>
[2] <http://php.net/manual/en/language.operators.comparison.php>
Previous Comments:
------------------------------------------------------------------------
[2015-03-30 12:41:14] greevex at gmail dot com
Description:
------------
---
From manual page: http://www.php.net/function.sort
---
I don't really think that it's problem in the sort, but you need to fix documentation with
adding information about sorting strings as numeric when values contains numbers only.
Test script:
---------------
$a = ["1500", "151"];
foreach($a as $item) { echo "Value is " . gettype($item) . PHP_EOL; }
echo "default:" . PHP_EOL;
sort($a);
var_dump($a);
echo "regular:" . PHP_EOL;
sort($a, SORT_REGULAR);
var_dump($a);
echo "numeric:" . PHP_EOL;
sort($a, SORT_NUMERIC);
var_dump($a);
echo "string:" . PHP_EOL;
sort($a, SORT_STRING);
var_dump($a);
Expected result:
----------------
Value is string
Value is string
default:
array(2) {
[0] =>
string(4) "1500"
[1] =>
string(3) "151"
}
regular:
array(2) {
[0] =>
string(4) "1500"
[1] =>
string(3) "151"
}
numeric:
array(2) {
[0] =>
string(3) "151"
[1] =>
string(4) "1500"
}
string:
array(2) {
[0] =>
string(4) "1500"
[1] =>
string(3) "151"
}
Actual result:
--------------
Value is string
Value is string
default:
array(2) {
[0] =>
string(3) "151"
[1] =>
string(4) "1500"
}
regular:
array(2) {
[0] =>
string(3) "151"
[1] =>
string(4) "1500"
}
numeric:
array(2) {
[0] =>
string(3) "151"
[1] =>
string(4) "1500"
}
string:
array(2) {
[0] =>
string(4) "1500"
[1] =>
string(3) "151"
}
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=69331&edit=1