debuggable

 
Contact Us
 

Sorting challenge

Posted on 25/10/07 by Felix Geisendörfer

Quick challenge,

lets say you have an array like this:

$products = array(
   array('Product' => array('ordering' => 5))
   , array('Product' => array('ordering' => 3))
   , array('Product' => array('ordering' => 9))
   , array('Product' => array('ordering' => 1))
);

and you want to iterate through your products by Product.ordering ASC. Whats the fastest way to do this? I just came up with this nifty little attempt, but I'd love to see some other solutions. (Rules: Don't use anything eval()'ish, but anything you find in Cake 1.2 can be used).

$order = array_flip(Set::extract($products, '{n}.Product.ordering'));
ksort($order);
while ($product = $products[array_shift($order)]) {
   debug($product);
}

-- Felix Geisendörfer aka the_undefined

 
&nsbp;

You can skip to the end and add a comment.

jickles  said on Oct 25, 2007:

How about this:

array_multisort($products, SORT_ASC);
foreach($products as $product)

debug($product);

I think it works.

jickles

Felix Geisendörfer said on Oct 25, 2007:

jickles: Hm, but that wouldn't work if there are user keys like Product.id in the array? I mean is there a way to specify what key you want to sort by using array_multisort?

Matias Lespiau  said on Oct 26, 2007:

@Felix:

I think array_multisort could be used like this:

$products = array(
array('Product' => array('ordering' => 5, 'id' => 2))

, array('Product' => array('ordering' => 9, 'id' => 9))

, array('Product' => array('ordering' => 1, 'id' => 11))

, array('Product' => array('ordering' => 5, 'id' => 3))

);

foreach ($products as $key => $val ) {
$ordering[$key] = $val['Product']['ordering'];

$id[$key] = $val['Product']['id'];

}

array_multisort($ordering, SORT_ASC, $id, SORT_ASC, $products);

print_r($products);

Nik Chankov said on Oct 26, 2007:

Also you can put type of sort, because some time you have to sort strings, another time you have to sort numbers. so the example above could be modified slightly and let's imagine that we have some name istead of ordering:

Example
$products = array(

array(’Product’ => array(’name’ => 'Matias', ‘id’ => 2))

, array(’Product’ => array(’name’ => 'Felix', ‘id’ => 9))

, array(’Product’ => array(’name’ => 'Jackles', ‘id’ => 11))

, array(’Product’ => array(’name’ => 'Nik', ‘id’ => 3))

);

array_multisort($name, SORT_ASC, SORT_STRING, $id, SORT_ASC, SORT_NUMERIC, $products);

print_r($products);

I can say that I've did a class in the past which sorts the multiple arrays for Html table presentation element and so far it still works as expected without any problem. ;)

David said on Oct 26, 2007:

$b) ? -1 : 1;
}

usort($products, 'sortProducts');

print_r($products);

?>

David said on Oct 26, 2007:

Wow, my previous post got munged. I'll try without the PHP tags.

function sortProducts($a, $b) {
$a = $a['Product']['ordering'];

$b = $b['Product']['ordering'];

if ($a == $b) return 0;

return ($a

PHPDeveloper.org said on Oct 26, 2007:

Felix Geisendorfer's Blog: Sorting Challenge...

...

David said on Oct 26, 2007:

Oh my .. I have no idea how to post code here, maybe convert it to HTML? ..

Well, my solution is here: http://davidp.dk/sorting.php

flurischt said on Oct 26, 2007:

I would also do it the "usort" way.

[...] Felix Geisendorfer has a quick little sorting example posted today showing on way to sort a multi-dimensional array. Quick challenge, lets say you have an array and you want to iterate through your products by [the key of each subarray in $products] Product.ordering ASC. Whats the fastest way to do this? [...]

Felix Geisendörfer said on Oct 26, 2007:

@everybody: You can paste code here: http://bin.cakephp.org/add and then post the link.

Thanks for the solutions provided so far, I'm wondering how my Set::extract / flip / sort mashup would do performance wise. The uSort solution might be the fastest.

[...] Felix Geisendorfer’s Blog: Sorting Challenge Posted in October 27th, 2007 by admin in News, PHP News Felix Geisendorfer has a quick little sorting example posted today showing on way to sort a multi-dimensional array. Quick challenge, lets say you have an array and you want to iterate through your products by [the key of each subarray in $products] Product.ordering ASC. Whats the fastest way to do this? [...]

This post is too old. We do not allow comments here anymore in order to fight spam. If you have real feedback or questions for the post, please contact us.