<p>Managed to do this by putting it all into an array:</p>

<pre><code>{% set productID = [] %}
{% set orders = craft.orders().limit(null).all() %}

{% for order in orders %}
    {% for item in order.lineItems %}
        {% set productID = productID |merge([ {
            'productId' : item.purchasable.product.customProductId,
            'groupName' : order.groupName
        }]) %}
    {% endfor %}
{% endfor %}

{% set productID = productID | supersort('sortAs', '{{ object.productId }} {{ object.groupName }}') %}

{% for theID in productID %}
    {{ theID.groupName }} - {{ theID.productId }}
{% endfor %}
</code></pre>

CC BY-SA 4.0
2019-08-05T11:02:07.640Z
2019-08-05T11:02:07.640Z
Amy
<p>I'm trying to sort my commerce orders by 2 variables, one being in the order loop, and the second being within the lineitems. I want to order by the lineitem variable first, and THEN the order variable. Something a bit like this:</p>

<p>Order - Group Name
LineItem - Product ID</p>

<p>I want to order by custom product ID asc and then Group Name alphabetically. So it would go a little like this:</p>

<p>ID: 1, GroupName: Aaa<br>
ID: 1, GroupName: Bbb<br>
ID: 2, GroupName: Aab</p>

<p>etc...</p>

<p>The current code I have is this:</p>

<pre><code>{% set orders = craft.orders().limit(null).all() %}
{% set orders = orders|supersort('sortAs', '{{ object.groupName }}') %}

{% for order in orders %}
    {% for item in order.lineItems %}
       {{ item.purchasable.product.customProductId }}
    {% endfor %}
{% endfor %}
</code></pre>

<p>Ideally, I'd need to do something along the lines of:</p>

<pre><code>{% set orders = orders|supersort('sortAs', '{{ item.purchasable.product.customProductId }} {{ object.groupName }}') %}
</code></pre>

<p>But that obviously doesn't work.</p>

<craft3><plugin-craftcommerce2><plugin-supersort>
Supersort - Sort multiple variables together
# Supersort - Sort multiple variables together

(Asked by: username_0 on 2019-08-05)

I'm trying to sort my commerce orders by 2 variables, one being in the order loop, and the second being within the lineitems. I want to order by the lineitem variable first, and THEN the order variable. Something a bit like this:
Order - Group Name
LineItem - Product ID
I want to order by custom product ID asc and then Group Name alphabetically. So it would go a little like this:
ID: 1, GroupName: Aaa
ID: 1, GroupName: Bbb
ID: 2, GroupName: Aab
etc...
The current code I have is this:
{% set orders = craft.orders().limit(null).all() %}
{% set orders = orders|supersort('sortAs', '{{ object.groupName }}') %}

{% for order in orders %}
    {% for item in order.lineItems %}
       {{ item.purchasable.product.customProductId }}
    {% endfor %}
{% endfor %}

Ideally, I'd need to do something along the lines of:
{% set orders = orders|supersort('sortAs', '{{ item.purchasable.product.customProductId }} {{ object.groupName }}') %}

But that obviously doesn't work.

**Tags:** craft3, plugin-craftcommerce2, plugin-supersort

---

## 1 answers

### Answer by username_0 on 2019-08-05. Score: 1 | Accepted answer

Managed to do this by putting it all into an array:
{% set productID = [] %}
{% set orders = craft.orders().limit(null).all() %}

{% for order in orders %}
    {% for item in order.lineItems %}
        {% set productID = productID |merge([ {
            'productId' : item.purchasable.product.customProductId,
            'groupName' : order.groupName
        }]) %}
    {% endfor %}
{% endfor %}

{% set productID = productID | supersort('sortAs', '{{ object.productId }} {{ object.groupName }}') %}

{% for theID in productID %}
    {{ theID.groupName }} - {{ theID.productId }}
{% endfor %}


---

