2

Here is target sql query: ...... order by field1 asc, price_index.min_price desc

And here is my code

$productCollection->getCollection()
         ->setOrder('field1', 'asc')
         ->setOrder('price', 'desc')

However in my result price always is first ordering field. can anyone help me, Please ? Thank you so much

Tan Dinh
  • 31
  • 2
  • 6

5 Answers5

7
$collection->getSelect()
    ->order('field1 asc');

or sort by multiple:

 $collection->getSelect()
    ->order(array('field1 asc', 'price desc'));
duttyman
  • 151
  • 1
  • 2
3

To sort using multiple Fields, you can chain calls to the Collection’s method addAttributeToSort()

$productCollection->getCollection()
         ->addAttributeToSort('field1', 'asc')
         ->addAttributeToSort('price', 'desc');
Manashvi Birla
  • 2,837
  • 3
  • 14
  • 28
  • Thank for your answer. Iam try this in get collection function but this is not work correctly. because prices will be first ordering filed in result. do you have other suggestions for me. thank you – Tan Dinh Jul 13 '15 at 04:23
3

on custom resource collection, use addOrder:

Mage::getModel('module/model')->getCollection()
    ->addOrder('first', 'ASC')
    ->addOrder('second', 'DESC')
    ->addOrder('other', 'DESC');
Rizal Fauzie
  • 31
  • 1
  • 1
  • 4
0

Using:

productCollection->getSelect()->reset(Zend_Db_Select::ORDER); 

Then:

productCollection->getSelect()
            ->order(.......) 

That code will resolve this problem ^ ^

Tan Dinh
  • 31
  • 2
  • 6
0

To sort multiple fields you can use

$collection = Mage::getModel(‘module/model_name’)->getCollection()
->addAttributeToSort(‘order’, ‘ASC’)
->addAttributeToSort(‘last_name’, ‘ASC’)
->addAttributeToSort(‘first_name’, ‘ASC’);
  • Thank for your answer. your code correctly with normal field. but with prices i must reset order from collection and add order again: `productCollection->getSelect() ->reset(Zend_Db_Select::ORDER) ->order('filed1'') ->order('field2'); ` – Tan Dinh Jul 16 '15 at 08:58