Reputation: 18692
The Magento collection sorting functions (e.g. Mage_Eav_Model_Entity_Collection_Abstract::addAttributeToSort
) work by adding an ORDER BY
clause to the SQL select statement. However, there are times when a collection has already been loaded and it is necessary to sort the collection.
It is certainly possible to use the toArray($fields)
function and then PHP array sorting functions (either native or user-defined), however this is a little clumsy. It also means that the objects in the collection are converted to "dumb" rows of values without magic getters/setters which can/are be implemented with algorithms, etc.
I'm wondering if there are more elegant/Magento-esque methods of sorting the collection.
Thanks,
Jonathan
Upvotes: 6
Views: 15053
Reputation: 9233
There is no proper way of doing it. But I think it is possible with using of Reflection. You can retrieve $_items property of collection object, sort them and set it back to the collection.
function sortCollection(Varien_Data_Collection $collection, callable $sorter) {
$collectionReflection = new ReflectionObject($collection);
$itemsPropertyReflection = $collectionReflection->getProperty('_items');
$itemsPropertyReflection->setAccessible(true); // Make it accessible
$collectionItems = $itemsPropertyReflection->getValue($collection);
usort($collectionItems, $sorter);
$itemsPropertyReflection->setValue($collection, $collectionItems);
$itemsPropertyReflection->setAccessible(false); // Return restriction back
return $collection;
}
Upvotes: 14
Reputation: 6306
The method of @Ivan Chepurnyi worked but returns a ReflectionObject object, in my case I needed a Varien_Data_Collection.
Here is what I did instead
$collectionItems = $collection->getItems();
usort($collectionItems, array($this, '_sortItems'));
$newCollection = new Varien_Data_Collection();
foreach ($collectionItems as $item) {
$newCollection->addItem($item);
}
var_dump($newCollection);
And in case here is the sorting method
public function _sortItems($a, $b)
{
$columnId = "your_column_that_you_need_to_sort";
$dir = "desc";
$al = strtolower($a->getData($columnId));
$bl = strtolower($b->getData($columnId));
if ($al == $bl) {
return 0;
}
if ($dir == 'asc') {
return ($al < $bl) ? -1 : 1;
} else {
return ($al > $bl) ? -1 : 1;
}
}
Upvotes: 3
Reputation: 12809
The above solution will work fine, but it's a LOT slower and more intensive than adding the sort to the query itself.
If you have a large collection you will use a huge amount of memory as you need to load an object (or objects) fore each result and store them all.
Using the collection Magento will only load one row at a time from the database which will be a lot more efficient than the above solution :-)
Upvotes: 0
Reputation: 18692
Another solution which works:
class Aligent_Navigation_Block_Dropdown extends Mage_Catalog_Block_Product_List {
public function getProductsByShortDesc(){
$data = $this->getLoadedProductCollection()->getItems(); //an array of objects
usort($data,array('Aligent_Navigation_Block_Dropdown','sortByShortDesc'));
return $data;
}
public static function sortByShortDesc($a, $b)
{
if($a->getShortDescription() == $b->getShortDescription()){ return 0 ; }
return ($a->getShortDescription() < $b->getShortDescription()) ? -1 : 1;
}
}
Upvotes: 2
Reputation: 37700
Here's a tip; A collection's clear
method unsets it's loaded flag, it allows you to change the sort or filters and run the new query.
I accidentally discovered it when answering load only configurable products.
Upvotes: 4