note 12113 deleted from function.sort by derick
| From: | derick@php.net | Date: | Wed, 31 Aug 2005 10:36:07 +0000 |
| Subject: | note 12113 deleted from function.sort by derick | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-94489@lists.php.net to get a copy of this message | ||
Note Submitter: spamt at maro dot net
----
I tried the above quicksort clip and it would send my script into an endless loop every time. A
little more investigation into sorting multi-dimentional arrays resulted in finding how to implement
the array_multisort function properly to do this. Array_multisort lets you take a multi-dimentional
array and sort by one column, keeping all your rows in line. Here's what I did:
With a two dimentional array of numbers stored in cat[X][Y], I wanted to sort by the second column
of numbers (descending), but have it keep the corresponding number in the first column lined up with
it's partner.
CAT: 0 1 <- X
__Y____________
: 1: 1 3
: 2: 0 6
: 3: 5 2
: 4: 8 4
To result in:
CAT: 0 1 <- X
__Y____________
: 1: 0 6
: 2: 8 4
: 3: 1 3
: 4: 5 2
array_multisort($cat[1], SORT_NUMERIC, SORT_DESC, $cat[0]);