Magento2 Order by clause in collection

You can try this

$giftColletion = $this->_giftFactory->getCollection();
$giftColletion->addFieldToFilter('store_id', 1);
$giftColletion->setOrder('position','ASC');

setOrder is use for sorting


According to \Magento\Catalog\Model\ResourceModel\Product\Collection you can use addAttributeToSort() method to sort your collection.

This has worked for me:

$collection = $this->_collection
    ->create()
    ->addAttributeToSelect(['sku', 'name', 'image'])
    ->addCategoryFilter($category)
    ->addAttributeToSort('name')
    ->setPageSize($limit);
return $collection;

Use whatever attribute you need your collection to be sorted by instead of 'name' in my example. You can also specify order direction as the second parameter, it defaults to ASC.