php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #69330 SORT_* DOESN'T WORK in array_multisort
Submitted: 2015-03-30 07:59 UTC Modified: 2015-03-30 08:56 UTC
From: to dot yashin at gmail dot com Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 5.5.23 OS: ubuntu
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: to dot yashin at gmail dot com
New email:
PHP Version: OS:

 

 [2015-03-30 07:59 UTC] to dot yashin at gmail dot com
Description:
------------
The second optional parameter array1_sort_order=SORT_DESC in array_multisort function does not work

Test script:
---------------
<?php

$result = array(
	array(
		'position' => 1,
		'col1' => 10,
		'col2' => 10,
	),
	array(
		'position' => 2,
		'col1' => 12,
		'col2' => 10,
	),
	array(
		'position' => 3,
		'col1' => 10,
		'col2' => 11,
	),
);

$col1_data = array();
$col2_data = array();
foreach ($result as $key => $row) {
	$col1_data[] = intval($row['col1']);
	$col2_data[] = intval($row['col2']);
}
// SORT_DESC !
array_multisort($result, SORT_DESC, SORT_NUMERIC, $col1_data, $col2_data);

foreach ($result as $row) {
	echo $row['position'], ' ';
}
echo "\n";
// Actual: 1 3 2
// Expected: 2 3 1


Expected result:
----------------
Sort Descending correct: 2 3 1

Actual result:
--------------
Sort Descending is not correct: 1 3 2

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2015-03-30 08:16 UTC] to dot yashin at gmail dot com
-Summary: SORT_* DOSNT WORK in array_multisort +Summary: SORT_* DOESN'T WORK in array_multisort
 [2015-03-30 08:16 UTC] to dot yashin at gmail dot com
//Grammar Nazi DOSNT => DOESN'T
 [2015-03-30 08:51 UTC] requinix@php.net
-Status: Open +Status: Not a bug
 [2015-03-30 08:51 UTC] requinix@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

array_multisort() works by sorting the arrays in the order given. The first argument is not special - it doesn't represent "the array to be sorted" or anything like that.

You want to sort $result by col1 then by col2. That means you need to provide those two _data* arrays in that order, and of course with the appropriate flags/options. $result doesn't contribute anything to the sort order, but must be included (as the final argument) so that it gets sorted.

Thus you should be calling array_multisort() as
  array_multisort(
    $col1_data, SORT_DESC, SORT_NUMERIC, // sort by col1 first,
    $col2_data, SORT_DESC, SORT_NUMERIC, // then by col2,
    $result // result won't contribute to the sort order but will be sorted
  );

http://3v4l.org/pWOLH
 [2015-03-30 08:56 UTC] to dot yashin at gmail dot com
Thank you very much!
 
PHP Copyright © 2001-2026 The PHP Group
All rights reserved.
Last updated: Wed Oct 07 11:00:02 2026 UTC