Skip to main content

How to Magento 2: sort values in collection with unique flag?

I have an array as collection and ordered it in ascending order with respect to expiry date as below:

$creditDetails = $this->_creditFactory->create()->addFieldToFilter('customer_id', 1)->setOrder('date_expires', 'asc');

I have some records which do not have any expiry date i.e NULL

I want such records to come in the collection at the end of or after the records which have some expiry date. Now, records without an expiry date are executing first.

How can I do this  ?

How to sort values in collection with unique flag?

I have an array as collection and ordered it in ascending order with respect to expiry date as below:

$creditDetails = $this->_creditFactory->create()->addFieldToFilter('customer_id', 1)->setOrder('date_expires', 'asc');

I have some records which do not have any expiry date i.e NULL

I want such records to come in collection at the end of or after the records which have some expiry date. Now, records without expiry date are executing first.

How can I do this  ?

Magento 2: sort values in collection with unique flag

I have an array as collection and ordered it in ascending order with respect to expiry date as below:

$creditDetails = $this->_creditFactory->create()->addFieldToFilter('customer_id', 1)->setOrder('date_expires', 'asc');

I have some records which do not have any expiry date i.e NULL

I want such records to come in the collection at the end of or after the records which have some expiry date. Now, records without an expiry date are executing first.

How can I do this?

Source Link
Ajwad Syed
  • 1.6k
  • 28
  • 62

How to sort values in collection with unique flag?

I have an array as collection and ordered it in ascending order with respect to expiry date as below:

$creditDetails = $this->_creditFactory->create()->addFieldToFilter('customer_id', 1)->setOrder('date_expires', 'asc');

I have some records which do not have any expiry date i.e NULL

I want such records to come in collection at the end of or after the records which have some expiry date. Now, records without expiry date are executing first.

How can I do this ?