Magento 2 – Custom Sort Order for Product Collection

collection;databasefiltermagento2sorting

I have product collection as below,

Array
(
    [0] => Array
        (
            [entity_id] => 1995
            [type_id] => configurable
            [sku] => WSH06
            [has_options] => 1
            [created_at] => 2016-05-04 11:23:45
            [updated_at] => 2016-05-04 11:23:45
            [cat_index_position] => 3
            [price] => 42.0000
            [tax_class_id] => 2
        )

    [1] => Array
        (
            [entity_id] => 2002
            [type_id] => configurable
            [sku] => WSH07
            [has_options] => 1
            [created_at] => 2016-05-04 11:23:45
            [updated_at] => 2016-05-04 11:23:45
            [cat_index_position] => 1
            [price] => 24.0000
            [tax_class_id] => 2
        )

    [2] => Array
        (
            [entity_id] => 2008
            [type_id] => configurable
            [sku] => WSH08
            [has_options] => 1
            [created_at] => 2016-05-04 11:23:45
            [updated_at] => 2016-05-04 11:23:45
            [cat_index_position] => 2
            [price] => 44.0000
            [tax_class_id] => 2
        )
)

Now i want to sort this collection based on one array which is having SKUs in it

Array
(
    [0] => WSH07
    [1] => WSH08
    [2] => WSH06
)

So that Collection should sort as below

Array
(

    [0] => Array
        (
            [entity_id] => 2002
            [type_id] => configurable
            [sku] => WSH07
            [has_options] => 1
            [created_at] => 2016-05-04 11:23:45
            [updated_at] => 2016-05-04 11:23:45
            [cat_index_position] => 1
            [price] => 24.0000
            [tax_class_id] => 2
        )

    [1] => Array
        (
            [entity_id] => 2008
            [type_id] => configurable
            [sku] => WSH08
            [has_options] => 1
            [created_at] => 2016-05-04 11:23:45
            [updated_at] => 2016-05-04 11:23:45
            [cat_index_position] => 2
            [price] => 44.0000
            [tax_class_id] => 2
        )

    [2] => Array
        (
            [entity_id] => 1995
            [type_id] => configurable
            [sku] => WSH06
            [has_options] => 1
            [created_at] => 2016-05-04 11:23:45
            [updated_at] => 2016-05-04 11:23:45
            [cat_index_position] => 3
            [price] => 42.0000
            [tax_class_id] => 2
        )    
)

I have disabled the magento default sort order, How i can achieve above sort order. Any help will be aprrecited,
Thanks!!!

Best Answer

Here is what i have done, Override "vendor/magento/module-catalog/Block/Product/ProductList/Toolbar.php" file to your module "app/code/Namespace/Catalog/Block/Rewrite/Product/ProductList/Toolbar.php"

public function setCollection($collection)
{
    $this->_collection = $collection;

    $this->_collection->setCurPage($this->getCurrentPage());

    // we need to set pagination only if passed value integer and more that 0
    $limit = (int)$this->getLimit();
    if ($limit) {
        $this->_collection->setPageSize($limit);
    }
    if ($this->getCurrentOrder()) {
        //Disabled the Magento Default Sort Order
        //$this->_collection->setOrder($this->getCurrentOrder(), $this->getCurrentDirection());

        $this->_collection;
    }
    //Sample static SKU array (Change it as dynamic)
    $skuarray = array(WSH07,WSH08,WSH06);

    //Setting Sort order which sort based on the array elements order
    $this->_collection->getSelect()->order("find_in_set(e.sku,'".implode(',',$skuarray)."')");

    return $this;
}