php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #69331 Default sort of numbers-as-a-string is not SORT_REGULAR
Submitted: 2015-03-30 12:41 UTC Modified: 2017-01-28 12:55 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:0 of 0 (0.0%)
From: greevex at gmail dot com Assigned:
Status: Open Package: Arrays related
PHP Version: 5.5.23 OS: CentOS 6.6
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: greevex at gmail dot com
New email:
PHP Version: OS:

 

 [2015-03-30 12:41 UTC] 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"
}


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2015-03-30 21:09 UTC] cmb@php.net
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>
 [2017-01-28 12:55 UTC] cmb@php.net
-Package: Documentation problem +Package: Arrays related
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 12:01:31 2024 UTC