Bug #69330 [Opn->Nab]: SORT_* DOESN'T WORK in array_multisort

From: Date: Mon, 30 Mar 2015 08:51:59 +0000
Subject: Bug #69330 [Opn->Nab]: SORT_* DOESN'T WORK in array_multisort
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-191710@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=69330&edit=1 ID: 69330 Updated by: requinix@php.net Reported by: to dot yashin at gmail dot com Summary: SORT_* DOESN'T WORK in array_multisort -Status: Open +Status: Not a bug Type: Bug Package: Scripting Engine problem Operating System: ubuntu PHP Version: 5.5.23 Block user comment: N Private report: N New Comment: 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 Previous Comments: ------------------------------------------------------------------------ [2015-03-30 08:16:33] to dot yashin at gmail dot com //Grammar Nazi DOSNT => DOESN'T ------------------------------------------------------------------------ [2015-03-30 07:59:09] 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 ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=69330&edit=1

« previous php.bugs (#191710) next »