{"id":305122,"date":"2021-09-11T09:43:21","date_gmt":"2021-09-11T09:43:21","guid":{"rendered":"https:\/\/webkul.com\/blog\/?p=305122"},"modified":"2024-06-17T10:36:20","modified_gmt":"2024-06-17T10:36:20","slug":"display-collection-records-in-a-specific-order-except-for-desc-or-asc-sort-order","status":"publish","type":"post","link":"https:\/\/webkul.com\/blog\/display-collection-records-in-a-specific-order-except-for-desc-or-asc-sort-order\/","title":{"rendered":"Display collection records in a specific order except for DESC or ASC sort order"},"content":{"rendered":"\n<p><strong>Display collection records in a specific order except for DESC or ASC sort order:<\/strong><\/p>\n\n\n\n<p>In Magento 2, Sometimes you need to display collection results in a specific order except for DESC or ASC sort orders in the query. <br>Please follow the below steps to do this:<\/p>\n\n\n\n<p>1. <a href=\"https:\/\/webkul.com\/blog\/create-hello-module-in-magento2\/\" target=\"_blank\" rel=\"noreferrer noopener\">Create a module.<br><\/a><br>2. Create layout xml file (routename_controller_action.xml) inside the app\/code\/Vendor\/Module\/view\/frontend\/layout directory.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\">&lt;?xml version=&quot;1.0&quot;?&gt;\n&lt;page xmlns:xsi=&quot;http:\/\/www.w3.org\/2001\/XMLSchema-instance&quot; layout=&quot;1column&quot; xsi:noNamespaceSchemaLocation=&quot;urn:magento:framework:View\/Layout\/etc\/page_configuration.xsd&quot;&gt;\n    &lt;referenceContainer name=&quot;content&quot;&gt;\n        &lt;block class=&quot;Vendor\\Module\\Block\\Demo\\Index&quot; \n            name=&quot;blogs_demo_index&quot; \n            template=&quot;Vendor_Module::demo.phtml&quot; \n            cacheable= &quot;false&quot;\/&gt;\n    &lt;\/referenceContainer&gt;\n&lt;\/page&gt;<\/pre>\n\n\n\n<p>3. Create a Block file (Index.php) inside the app\/code\/Vendor\/Module\/Block directory. In which, we will get the product collection.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\">&lt;?php\n\/**\n * Webkul Software.\n *\n * @category  Webkul\n * @package   Vendor_Module\n * @author    Webkul\n * @copyright Copyright (c) Webkul Software Private Limited (https:\/\/webkul.com)\n * @license   https:\/\/store.webkul.com\/license.html\n *\/\nnamespace Vendor\\Module\\Block\\Demo;\n\nclass Index extends \\Magento\\Framework\\View\\Element\\Template\n{\n    \/**\n     * Construct\n     * \n     * @param \\Magento\\Framework\\View\\Element\\Template\\Context $context\n     * @param \\Magento\\Catalog\\Model\\ResourceModel\\Product\\CollectionFactory $productCollection\n     *\/\n    public function __construct(\n        \\Magento\\Framework\\View\\Element\\Template\\Context $context,\n        \\Magento\\Catalog\\Model\\ResourceModel\\Product\\CollectionFactory $productCollection\n    ) {\n        $this-&gt;productCollection = $productCollection;\n        parent::__construct($context);\n    }\n\n    \/**\n     * Get product collection\n     *\/\n    public function getProductCollection()\n    {\n        $productIds = &#091;2, 20, 1, 9, 19]; \/\/unsorted product Ids\n\n        $filterAttributes = &#091;\n            &quot;entity_id&quot;,\n            &quot;name&quot;,\n            &quot;price&quot;,\n            &quot;sku&quot;,\n        ];\n        $collection = $this-&gt;productCollection-&gt;create()\n            -&gt;addAttributeToSelect($filterAttributes)\n            -&gt;addStoreFilter()\n            -&gt;addAttributeToFilter(&quot;entity_id&quot;, &#091;&quot;in&quot;=&gt;$productIds]);\n\n        $cases = &quot;CASE &quot;;\n        for ($i = 0; $i &lt; count($productIds); $i++) {\n            $cases .= &#039;WHEN e.entity_id = &#039;.$productIds&#091;$i].&#039; THEN &#039;.$i.&#039; &#039;;\n        }\n        $cases .= &quot;END&quot;;\n\n        $collection-&gt;getSelect()-&gt;order(new \\Zend_Db_Expr($cases));\n        return $collection;\n    }\n}<\/pre>\n\n\n\n<p>4. Create template file(demo.phtml) inside the app\/code\/Vendor\/Module\/view\/frontend\/templates\/ directory. In this file, we will get the product collection from Block and display the record in the table.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\">&lt;table&gt;\n    &lt;thead&gt;\n        &lt;th&gt;Product Id&lt;\/th&gt;\n        &lt;th&gt;Name&lt;\/th&gt;\n        &lt;th&gt;SKU&lt;\/th&gt;\n        &lt;th&gt;Price&lt;\/th&gt;\n    &lt;\/thead&gt;\n    &lt;tbody&gt;\n&lt;?php\n$collection = $block-&gt;getProductCollection();\nif (!empty($collection)) {\n    foreach ($collection as $each) {\n?&gt;\n    &lt;tr&gt;\n        &lt;td&gt;&lt;?= $each-&gt;getId() ?&gt;&lt;\/td&gt;\n        &lt;td&gt;&lt;?= $each-&gt;getName() ?&gt;&lt;\/td&gt;\n        &lt;td&gt;&lt;?= $each-&gt;getSku() ?&gt;&lt;\/td&gt;\n        &lt;td&gt;&lt;?= $each-&gt;getPrice() ?&gt;&lt;\/td&gt;\n    &lt;\/tr&gt;\n&lt;?php\n    }\n} else {\n?&gt;\n    &lt;tr&gt;&lt;td colspan=&quot;4&quot;&gt;No Record Found&lt;\/td&gt;&lt;\/tr&gt;\n&lt;?php\n}\n?&gt;\n    &lt;\/tbody&gt;\n&lt;\/table&gt;<\/pre>\n\n\n\n<p>5. Create Controller Index.php file inside of app\/code\/Vendor\/Module\/Controller\/Demo directory.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\">&lt;?php\n\/**\n * Webkul Software.\n *\n * @category  Webkul\n * @package   Vendor_Module\n * @author    Webkul\n * @copyright Copyright (c) Webkul Software Private Limited (https:\/\/webkul.com)\n * @license   https:\/\/store.webkul.com\/license.html\n *\/\nnamespace Vendor\\Module\\Controller\\Demo;\n\nuse Magento\\Framework\\App\\Action\\Action;\nuse Magento\\Framework\\App\\Action\\Context;\nuse Magento\\Framework\\View\\Result\\PageFactory;\n\nclass Index extends Action\n{\n    \/**\n     * @var PageFactory\n     *\/\n    protected $_resultPageFactory;\n\n    \/**\n     * initialization\n     *\n     * @param Context $context\n     * @param PageFactory $resultPageFactory\n     *\/\n    public function __construct(\n        Context $context,\n        PageFactory $resultPageFactory\n    ) {\n        $this-&gt;_resultPageFactory = $resultPageFactory;\n        parent::__construct($context);\n    }\n    \n    public function execute()\n    {\n        $resultPage = $this-&gt;_resultPageFactory-&gt;create();\n        $resultPage-&gt;getConfig()-&gt;getTitle()-&gt;set(__(&quot;Collection&quot;));\n        \n        return $resultPage;\n    }\n}<\/pre>\n\n\n\n<p>6. When you will hit the controller on the browser, you will get the result in the following image.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"1200\" height=\"543\" src=\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/09\/download-1-1-1200x543.png\" alt=\"download-1-1\" class=\"wp-image-305145\" srcset=\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/09\/download-1-1-1200x543.png 1200w, https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/09\/download-1-1-300x136.png 300w, https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/09\/download-1-1-250x113.png 250w, https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/09\/download-1-1-768x348.png 768w, https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/09\/download-1-1-1536x695.png 1536w, https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/09\/download-1-1.png 1814w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" loading=\"lazy\" \/><figcaption class=\"wp-element-caption\">Collection Results in Specific Sort<\/figcaption><\/figure>\n\n\n\n<p>Hope this will be helpful. Thanks \ud83d\ude42<br>You can read more blogs:<br><a href=\"https:\/\/webkul.com\/blog\/profiler-in-magento-2\/\" target=\"_blank\" rel=\"noreferrer noopener\">Profiler in Magento2<\/a><br><a href=\"https:\/\/webkul.com\/blog\/creating-post-method-controller-in-magento-2-3\/\" target=\"_blank\" rel=\"noreferrer noopener\">Creating POST Method Controller in Magento 2.3<\/a><a href=\"https:\/\/webkul.com\/blog\/wp-admin\/post.php?post=305122&amp;action=edit\" target=\"_blank\" rel=\"noreferrer noopener\"><br><\/a><a href=\"https:\/\/webkul.com\/blog\/how-to-create-custom-customer-address-attribute-in-magento-2\/\" target=\"_blank\" rel=\"noreferrer noopener\">How to create custom customer address attribute in Magento 2<\/a><br><a href=\"https:\/\/webkul.com\/blog\/inspecting-developer-tools-in-ipad-device\/\" target=\"_blank\" rel=\"noreferrer noopener\">Inspecting developer tools in iPad Device<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Display collection records in a specific order except for DESC or ASC sort order: In Magento 2, Sometimes you need to display collection results in a specific order except for DESC or ASC sort orders in the query. Please follow the below steps to do this: 1. Create a module.2. Create layout xml file (routename_controller_action.xml) <a href=\"https:\/\/webkul.com\/blog\/display-collection-records-in-a-specific-order-except-for-desc-or-asc-sort-order\/\">[&#8230;]<\/a><\/p>\n","protected":false},"author":249,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9121,302],"tags":[2070,12025],"class_list":["post-305122","post","type-post","status-publish","format-standard","hentry","category-magento-2","category-magento2","tag-magento2","tag-specific-sort-order"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Display collection records in specific order except DESC or ASC sort order<\/title>\n<meta name=\"description\" content=\"Display collection records in a specific order except for DESC or ASC sort order: In Magento 2, Sometimes you need to display the collection results in a specific order except for DESC or ASC sort orders in the query. Please follow the below steps to do this: When you are getting the product collection, you have to need to add CASE for desired column(for example entity_id) in the query.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/webkul.com\/blog\/display-collection-records-in-a-specific-order-except-for-desc-or-asc-sort-order\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Display collection record in a specific sort order except for DESC or ASC\" \/>\n<meta property=\"og:description\" content=\"Sometimes, we need to display the record in specific sort order, here you can see how can we do it.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/webkul.com\/blog\/display-collection-records-in-a-specific-order-except-for-desc-or-asc-sort-order\/\" \/>\n<meta property=\"og:site_name\" content=\"Webkul Blog\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/webkul\/\" \/>\n<meta property=\"article:published_time\" content=\"2021-09-11T09:43:21+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-06-17T10:36:20+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/webkul.com\/blog\/wp-content\/uploads\/2021\/09\/download-1-1-1200x543.png\" \/>\n<meta name=\"author\" content=\"Khushboo Sahu\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:title\" content=\"Display collection record in a specific sort order except for DESC or ASC\" \/>\n<meta name=\"twitter:description\" content=\"Sometimes, we need to display the record in specific sort order, here you can see how can we do it.\" \/>\n<meta name=\"twitter:creator\" content=\"@webkul\" \/>\n<meta name=\"twitter:site\" content=\"@webkul\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Khushboo Sahu\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/webkul.com\/blog\/display-collection-records-in-a-specific-order-except-for-desc-or-asc-sort-order\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/webkul.com\/blog\/display-collection-records-in-a-specific-order-except-for-desc-or-asc-sort-order\/\"},\"author\":{\"name\":\"Khushboo Sahu\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/f94b8f53397bf85810761d76c98fadca\"},\"headline\":\"Display collection records in a specific order except for DESC or ASC sort order\",\"datePublished\":\"2021-09-11T09:43:21+00:00\",\"dateModified\":\"2024-06-17T10:36:20+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/webkul.com\/blog\/display-collection-records-in-a-specific-order-except-for-desc-or-asc-sort-order\/\"},\"wordCount\":207,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/webkul.com\/blog\/#organization\"},\"image\":{\"@id\":\"https:\/\/webkul.com\/blog\/display-collection-records-in-a-specific-order-except-for-desc-or-asc-sort-order\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/webkul.com\/blog\/wp-content\/uploads\/2021\/09\/download-1-1-1200x543.png\",\"keywords\":[\"Magento2\",\"specific sort order\"],\"articleSection\":[\"Magento 2\",\"Magento2\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/webkul.com\/blog\/display-collection-records-in-a-specific-order-except-for-desc-or-asc-sort-order\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/webkul.com\/blog\/display-collection-records-in-a-specific-order-except-for-desc-or-asc-sort-order\/\",\"url\":\"https:\/\/webkul.com\/blog\/display-collection-records-in-a-specific-order-except-for-desc-or-asc-sort-order\/\",\"name\":\"Display collection records in specific order except DESC or ASC sort order\",\"isPartOf\":{\"@id\":\"https:\/\/webkul.com\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/webkul.com\/blog\/display-collection-records-in-a-specific-order-except-for-desc-or-asc-sort-order\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/webkul.com\/blog\/display-collection-records-in-a-specific-order-except-for-desc-or-asc-sort-order\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/webkul.com\/blog\/wp-content\/uploads\/2021\/09\/download-1-1-1200x543.png\",\"datePublished\":\"2021-09-11T09:43:21+00:00\",\"dateModified\":\"2024-06-17T10:36:20+00:00\",\"description\":\"Display collection records in a specific order except for DESC or ASC sort order: In Magento 2, Sometimes you need to display the collection results in a specific order except for DESC or ASC sort orders in the query. Please follow the below steps to do this: When you are getting the product collection, you have to need to add CASE for desired column(for example entity_id) in the query.\",\"breadcrumb\":{\"@id\":\"https:\/\/webkul.com\/blog\/display-collection-records-in-a-specific-order-except-for-desc-or-asc-sort-order\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/webkul.com\/blog\/display-collection-records-in-a-specific-order-except-for-desc-or-asc-sort-order\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/webkul.com\/blog\/display-collection-records-in-a-specific-order-except-for-desc-or-asc-sort-order\/#primaryimage\",\"url\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/09\/download-1-1.png\",\"contentUrl\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/09\/download-1-1.png\",\"width\":1814,\"height\":821,\"caption\":\"download-1-1\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/webkul.com\/blog\/display-collection-records-in-a-specific-order-except-for-desc-or-asc-sort-order\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/webkul.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Display collection record in a specific sort order except for DESC or ASC\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/webkul.com\/blog\/#website\",\"url\":\"https:\/\/webkul.com\/blog\/\",\"name\":\"Webkul Blog\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/webkul.com\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/webkul.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/webkul.com\/blog\/#organization\",\"name\":\"WebKul Software Private Limited\",\"url\":\"https:\/\/webkul.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png\",\"contentUrl\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png\",\"width\":380,\"height\":380,\"caption\":\"WebKul Software Private Limited\"},\"image\":{\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/webkul\/\",\"https:\/\/x.com\/webkul\",\"https:\/\/www.instagram.com\/webkul\/\",\"https:\/\/www.linkedin.com\/company\/webkul\",\"https:\/\/www.youtube.com\/user\/webkul\/\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/f94b8f53397bf85810761d76c98fadca\",\"name\":\"Khushboo Sahu\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/cabac965df656d114e6bf340df07518c990eda03bb09265dbd5c17f1097adaae?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Feva.png&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/cabac965df656d114e6bf340df07518c990eda03bb09265dbd5c17f1097adaae?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Feva.png&r=g\",\"caption\":\"Khushboo Sahu\"},\"url\":\"https:\/\/webkul.com\/blog\/author\/khushboo-sahu062\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Display collection records in specific order except DESC or ASC sort order","description":"Display collection records in a specific order except for DESC or ASC sort order: In Magento 2, Sometimes you need to display the collection results in a specific order except for DESC or ASC sort orders in the query. Please follow the below steps to do this: When you are getting the product collection, you have to need to add CASE for desired column(for example entity_id) in the query.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/webkul.com\/blog\/display-collection-records-in-a-specific-order-except-for-desc-or-asc-sort-order\/","og_locale":"en_US","og_type":"article","og_title":"Display collection record in a specific sort order except for DESC or ASC","og_description":"Sometimes, we need to display the record in specific sort order, here you can see how can we do it.","og_url":"https:\/\/webkul.com\/blog\/display-collection-records-in-a-specific-order-except-for-desc-or-asc-sort-order\/","og_site_name":"Webkul Blog","article_publisher":"https:\/\/www.facebook.com\/webkul\/","article_published_time":"2021-09-11T09:43:21+00:00","article_modified_time":"2024-06-17T10:36:20+00:00","og_image":[{"url":"https:\/\/webkul.com\/blog\/wp-content\/uploads\/2021\/09\/download-1-1-1200x543.png","type":"","width":"","height":""}],"author":"Khushboo Sahu","twitter_card":"summary_large_image","twitter_title":"Display collection record in a specific sort order except for DESC or ASC","twitter_description":"Sometimes, we need to display the record in specific sort order, here you can see how can we do it.","twitter_creator":"@webkul","twitter_site":"@webkul","twitter_misc":{"Written by":"Khushboo Sahu","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/webkul.com\/blog\/display-collection-records-in-a-specific-order-except-for-desc-or-asc-sort-order\/#article","isPartOf":{"@id":"https:\/\/webkul.com\/blog\/display-collection-records-in-a-specific-order-except-for-desc-or-asc-sort-order\/"},"author":{"name":"Khushboo Sahu","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/f94b8f53397bf85810761d76c98fadca"},"headline":"Display collection records in a specific order except for DESC or ASC sort order","datePublished":"2021-09-11T09:43:21+00:00","dateModified":"2024-06-17T10:36:20+00:00","mainEntityOfPage":{"@id":"https:\/\/webkul.com\/blog\/display-collection-records-in-a-specific-order-except-for-desc-or-asc-sort-order\/"},"wordCount":207,"commentCount":0,"publisher":{"@id":"https:\/\/webkul.com\/blog\/#organization"},"image":{"@id":"https:\/\/webkul.com\/blog\/display-collection-records-in-a-specific-order-except-for-desc-or-asc-sort-order\/#primaryimage"},"thumbnailUrl":"https:\/\/webkul.com\/blog\/wp-content\/uploads\/2021\/09\/download-1-1-1200x543.png","keywords":["Magento2","specific sort order"],"articleSection":["Magento 2","Magento2"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/webkul.com\/blog\/display-collection-records-in-a-specific-order-except-for-desc-or-asc-sort-order\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/webkul.com\/blog\/display-collection-records-in-a-specific-order-except-for-desc-or-asc-sort-order\/","url":"https:\/\/webkul.com\/blog\/display-collection-records-in-a-specific-order-except-for-desc-or-asc-sort-order\/","name":"Display collection records in specific order except DESC or ASC sort order","isPartOf":{"@id":"https:\/\/webkul.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/webkul.com\/blog\/display-collection-records-in-a-specific-order-except-for-desc-or-asc-sort-order\/#primaryimage"},"image":{"@id":"https:\/\/webkul.com\/blog\/display-collection-records-in-a-specific-order-except-for-desc-or-asc-sort-order\/#primaryimage"},"thumbnailUrl":"https:\/\/webkul.com\/blog\/wp-content\/uploads\/2021\/09\/download-1-1-1200x543.png","datePublished":"2021-09-11T09:43:21+00:00","dateModified":"2024-06-17T10:36:20+00:00","description":"Display collection records in a specific order except for DESC or ASC sort order: In Magento 2, Sometimes you need to display the collection results in a specific order except for DESC or ASC sort orders in the query. Please follow the below steps to do this: When you are getting the product collection, you have to need to add CASE for desired column(for example entity_id) in the query.","breadcrumb":{"@id":"https:\/\/webkul.com\/blog\/display-collection-records-in-a-specific-order-except-for-desc-or-asc-sort-order\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/webkul.com\/blog\/display-collection-records-in-a-specific-order-except-for-desc-or-asc-sort-order\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/webkul.com\/blog\/display-collection-records-in-a-specific-order-except-for-desc-or-asc-sort-order\/#primaryimage","url":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/09\/download-1-1.png","contentUrl":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/09\/download-1-1.png","width":1814,"height":821,"caption":"download-1-1"},{"@type":"BreadcrumbList","@id":"https:\/\/webkul.com\/blog\/display-collection-records-in-a-specific-order-except-for-desc-or-asc-sort-order\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/webkul.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Display collection record in a specific sort order except for DESC or ASC"}]},{"@type":"WebSite","@id":"https:\/\/webkul.com\/blog\/#website","url":"https:\/\/webkul.com\/blog\/","name":"Webkul Blog","description":"","publisher":{"@id":"https:\/\/webkul.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/webkul.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/webkul.com\/blog\/#organization","name":"WebKul Software Private Limited","url":"https:\/\/webkul.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png","contentUrl":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png","width":380,"height":380,"caption":"WebKul Software Private Limited"},"image":{"@id":"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/webkul\/","https:\/\/x.com\/webkul","https:\/\/www.instagram.com\/webkul\/","https:\/\/www.linkedin.com\/company\/webkul","https:\/\/www.youtube.com\/user\/webkul\/"]},{"@type":"Person","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/f94b8f53397bf85810761d76c98fadca","name":"Khushboo Sahu","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/cabac965df656d114e6bf340df07518c990eda03bb09265dbd5c17f1097adaae?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Feva.png&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/cabac965df656d114e6bf340df07518c990eda03bb09265dbd5c17f1097adaae?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Feva.png&r=g","caption":"Khushboo Sahu"},"url":"https:\/\/webkul.com\/blog\/author\/khushboo-sahu062\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/305122","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/users\/249"}],"replies":[{"embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/comments?post=305122"}],"version-history":[{"count":20,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/305122\/revisions"}],"predecessor-version":[{"id":448257,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/305122\/revisions\/448257"}],"wp:attachment":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/media?parent=305122"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/categories?post=305122"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/tags?post=305122"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}