php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #12848 asort-SORT_NUMERIC inconsistent with non numeric values
Submitted: 2001-08-19 18:57 UTC Modified: 2001-10-21 01:51 UTC
From: kzi at gmx dot net Assigned:
Status: Closed Package: Arrays related
PHP Version: 4.0.6 OS: win98
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: kzi at gmx dot net
New email:
PHP Version: OS:

 

 [2001-08-19 18:57 UTC] kzi at gmx dot net
If asort() is used with the sort flag SORT_NUMERIC and the values ar non numeric, the sort order of those values should remain unchanged in principle. But in arrays with few of such non numeric items (app. < 10), the first item is generally moved to the bottom, while in arrays with more items, one element from somewhere in the middle is moved to the top.

example:
$q = array(1=>'A',2=>'B',3=>'C',4=>'D',5=>'E');
asort($q,SORT_NUMERIC);

produces the value sequence B,C,D,E,A


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2001-10-21 01:51 UTC] sniper@php.net
Can not reproduce with PHP 4.1.0RC1.

--Jani

 [2002-04-28 08:03 UTC] developer at i-space dot org
i have the same problem :
using 6 array : $years, $months, $ids, $titles, $urls, $dates
and doing :
$ar = array($years, $months, $ids, $titles, $urls, $dts);
array_multisort($ar[0], SORT_DESC, SORT_NUMERIC, $ar[1], SORT_NUMERIC, SORT_DESC, $ar[2], $ar[3], $ar[4], $ar[5]);
which results "Warning: Array sizes are inconsistent in ...". even with SORT_REGULAR it gives the same error. but it sorts them which is pretty funny :)
i suppress it with @ and it sorts them without showing warnings - the only way it works
p.s. it shwo that warning from time to time - not always.
 [2002-09-27 06:44 UTC] tadgher at yahoo dot com
I've also encountered this problem.

Version:
PHP Version 4.2.3

Environment:
FreeBSD 4.7-PRERELEASE #1: Mon Sep 2 12:14:18 IST 2002  

Configure Command:
 './configure' '--with-apxs=/usr/local/sbin/apxs' '--with-config-file-path=/usr/local/etc' '--enable-versioning' '--with-regex=system' '--without-gd' '--without-mysql' '--with-mhash=/usr/local' '--with-pgsql=/usr/local' '--with-ldap=/usr/local' '--enable-xslt' '--with-xslt-sablot=/usr/local' '--enable-trans-sid' '--with-expat-dir=/usr/local' '--with-iconv=/usr/local' '--prefix=/usr/local' 'i386-portbld-freebsd4.7'

(built from FreeBSD ports collection)

Testcase:
// create an array
$array = array();
for ($i = 1; $i < 25; $i++) {
  array_push($array,"Dublin $i");
}
// randomise it
$rand_array = array();
srand ((float) microtime() * 10000000);
foreach (array_rand($array,sizeof($array)) as $index) {
  array_push($rand_array,$array[$index]);
}

// get it sorted by string
$array_sorted_str = $rand_array;
sort($array_sorted_str,SORT_STRING);
// get it sorted numerically
$array_sorted_num = $rand_array;
sort($array_sorted_num,SORT_NUMERIC);

echo "Original array";
echo "<ul>";
foreach ($array as $v) {
  echo "<li>$v</li>";
}
echo "</ul>";

echo "Randomised array";
echo "<ul>";
foreach ($rand_array as $v) {
  echo "<li>$v</li>";
}
echo "</ul>";

echo "array sorted by string";
echo "<ul>";
foreach ($array_sorted_str as $v) {
  echo "<li>$v</li>";
}
echo "</ul>";

echo "array sorted numerically";
echo "<ul>";
foreach ($array_sorted_num as $v) {
  echo "<li>$v</li>";
}
echo "</ul>";

I would expect the numeric sort to give me Dublin 1, Dublin 2...Dublin 11,Dublin 12...Dublin 20 etc. 

While it does change the array, it appears to do so in a random fashion (at least I cannot understand it).

Tadgh
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Tue May 06 02:01:28 2025 UTC