It is possible to add element to the collection as the first element, similar to this method which adds element to the end?
$product = Mage::getModel('catalog/product')->load(15879);
$productCollection->addItem($product);
collection;magento-1
It is possible to add element to the collection as the first element, similar to this method which adds element to the end?
$product = Mage::getModel('catalog/product')->load(15879);
$productCollection->addItem($product);
Best Answer
Unfortunately not,
Varien_Data_Collection
is not very flexible in this respect. To reorder items in a collection you need to remove them and add them again in the desired order.In your specific case:
Update: Performance
Since there are objections regarding performance, I did a little benchmark on a real product database. Note that loading a collection with 20K products is something you should never actually do but this is just to prove the point that iterating over the loaded arrays twice does not add a significant overhead:
Test Script
Output
And with a more realistic amount of 100 loaded products: