Magento2 sorting collection order
Collections in Magento2 can be a real pain. Was having problems with sorting a category getProductCollection collection into position order! The products were just displaying in random order(or rather ID number order). $categoryId=891; $category = $this->_categoryFactory->create(); $category->load($categoryId); $products = $category->getProductCollection();$products->addAttributeToSelect(‘*’);$products->setOrder(“cat_index_position”,’ASC’);