<p>Worth mentioning first that Element Queries have a <code>fixedOrder</code> parameter available to them (<a href="https://craftcms.com/docs/commerce/4.x/products-variants.html#product-fixedorder" rel="nofollow noreferrer">https://craftcms.com/docs/commerce/4.x/products-variants.html#product-fixedorder</a>)... however from looking at the docs I think that might only work if you also specify the element IDs instead of slugs. If that's a possibility for your setup it might be the best solution.</p>
<p>If not, then you could instead treat your result set as a hash map/lookup table and loop through your preferred slugs variable something like this (untested):</p>
<pre class="lang-twig prettyprint-override"><code>{% set slugs = ['A1', 'B2', 'C3'] %}
{% set products = craft.commerce.products.slug(slugs).all() %}
    
&lt;ul&gt;
    {% for slug in slugs %}
        {% set product = products|filter(item =&gt; item.slug == slug)[0] %}
        &lt;li&gt;{{ product.title }}&lt;/li&gt;
    {% endfor %}
&lt;/ul&gt;
</code></pre>

CC BY-SA 4.0
2022-09-21T09:53:26.393Z
How can I convert ```{% set slugs = "A1,B2,C3" %}```  to {% set slugs = ['A1', 'B2', 'C3'] %} ? 
Split function is not working
Jaimini Solanki
CC BY-SA 4.0
2022-09-21T13:11:40.237Z
If the `split` filter isn't working you'll need to figure out why. Try dumping out your input string to see exactly what you have. Where is the string coming from?
James Smith
CC BY-SA 4.0
2022-09-22T05:10:58.553Z
Split function is working now but ```{% set product = products|filter(item => item.slug == slug)[0] %}``` line is showing error about array_filter
Jaimini Solanki
CC BY-SA 4.0
2022-09-22T08:28:42.327Z
Would need to see the exact error to help. Maybe try removing the `[0]` from the end.
James Smith
CC BY-SA 4.0
2022-09-20T14:17:44.963Z
2022-09-20T14:17:44.963Z
James Smith
<p>I am fetching data using the custom slug in craft commerce products but I am not getting the sequence of the products which I am providing in the custom slug</p>
<p>Here is the code for that.</p>
<p><code>slugs = &quot;A1,B2,C3&quot;;</code></p>
<p><code>{% set products = craft.commerce.products({slug: slugs}) %}</code></p>
<p>Results I am getting</p>
<p><code>B2,C3,A1</code></p>
<p>I want the results</p>
<p><code>A1,B2,C3</code></p>
<p>Also, the Split function is not working over here.</p>

<sort>
How to sort database values using other variable
# How to sort database values using other variable

(Asked by: username_0 on 2022-09-20)

I am fetching data using the custom slug in craft commerce products but I am not getting the sequence of the products which I am providing in the custom slug

Here is the code for that.

`slugs = "A1,B2,C3";`

`{% set products = craft.commerce.products({slug: slugs}) %}`

Results I am getting

`B2,C3,A1`

I want the results

`A1,B2,C3`

Also, the Split function is not working over here.

**Tags:** sort


---

## 1 answers

### Answer by username_1 on 2022-09-20. Score: 1 | Accepted answer

Worth mentioning first that Element Queries have a fixedOrder parameter available to them (https://craftcms.com/docs/commerce/4.x/products-variants.html#product-fixedorder)... however from looking at the docs I think that might only work if you also specify the element IDs instead of slugs. If that's a possibility for your setup it might be the best solution.
If not, then you could instead treat your result set as a hash map/lookup table and loop through your preferred slugs variable something like this (untested):
{% set slugs = ['A1', 'B2', 'C3'] %}
{% set products = craft.commerce.products.slug(slugs).all() %}
    

    {% for slug in slugs %}
        {% set product = products|filter(item => item.slug == slug)[0] %}
        {{ product.title }}
    {% endfor %}

> **Comments:**
> **username_0**: How can I convert ```{% set slugs = "A1,B2,C3" %}```  to {% set slugs = ['A1', 'B2', 'C3'] %} ?  Split function is not working  
> **username_1**: If the `split` filter isn't working you'll need to figure out why. Try dumping out your input string to see exactly what you have. Where is the string coming from?  
> **username_0**: Split function is working now but ```{% set product = products|filter(item => item.slug == slug)[0] %}``` line is showing error about array_filter  
> **username_1**: Would need to see the exact error to help. Maybe try removing the `[0]` from the end.  

---

