Magento2 Layered Navigation – Sort Attribute Options by Name Instead of Number of Products

layered-navigationmagento2magento2.2.4product-attributesort

My theme is child of magento blank.
On default magento 2 layered navigation sort labels of attribute in layered navigation by Count number of products that exist with this attribute.

I need to change this alphabetically sort order.

Example:
Colours:
Red (10)
Blue (3)
Yellow (2)
Change to:
Blue (3)
Red (10)
Yellow (2)

Where should I look?
In module_layered_navigation? or Magento\Framework\Data\OptionSourceInterface? or somwhere else?

I found this in vendor/magento/module-catalog/Model/Layer/Filter/AbstractFilter.php

     * Create filter item object
 *
 * @param   string $label
 * @param   mixed $value
 * @param   int $count
 * @return  \Magento\Catalog\Model\Layer\Filter\Item
 */
protected function _createItem($label, $value, $count = 0)
{
    return $this->_filterItemFactory->create()
        ->setFilter($this)
        ->setLabel($label)
        ->setValue($value)
        ->setCount($count);
}

Maybe I could add somewhere usort:

usort($this, function($a, $b) {
if($a['order']==$b['order']) return 0;
return $a['order'] < $b['order']?1:-1;

});

Best Answer

It looks like it works on the newest Magento 2.2.4

Sort order can be applied manually in the backend.

Edit attribute => Properties=> Sort using drag & drop

Easy way.

Related Topic