<p>If the field you're ordering by is identical across multiple products, the order in which those are returned is undefined (i.e. determined by the database, essentially random). To achieve a consistent sort order, you need to specify a second field to sort by if the first one is the same. For example, you could just sort products with the same <code>productOrderInCategory</code> alphabetically by title:</p>
<pre><code>{% set products = products.relatedTo(category)
                    .orderBy('productOrderInCategory, title DESC')
                    .inReverse() %}
</code></pre>
<p>The syntax for sorting by multiple fields is <a href="https://www.yiiframework.com/doc/guide/2.0/en/db-query-builder#order-by" rel="nofollow noreferrer">documented here</a>.</p>

CC BY-SA 4.0
2021-07-30T11:30:10.797Z
Thank you, that helped.
Piotr Pogorzelski
CC BY-SA 4.0
2021-07-30T09:34:33.340Z
2021-07-30T09:34:33.340Z
MoritzLost
<p>I have element query that orders products by custom field which is numeric. Default value of this field is &quot;1&quot;.</p>
<pre><code>{% set products = products.relatedTo(category).orderBy('productOrderInCategory').inReverse() %}
</code></pre>
<p>All is fine, when i do this</p>
<pre><code>{% set productList = products.all() %}
</code></pre>
<p>When i use pagination, order of products with same &quot;productOrderInCategory&quot; field value randomly changes each time page loads.</p>
<pre><code>{% paginate products.limit(32) as pageInfo, paginatedProducts %}
</code></pre>
<p>So, product with larger productOrderInCategory value will always be first, but multiple products with same productOrderInCategory value will be randomly shuffled each time page loads.</p>
<p>How can i fix that?</p>

<elementquery>
Ordering products using custom field and pagination
# Ordering products using custom field and pagination

(Asked by: username_0 on 2021-07-30)

I have element query that orders products by custom field which is numeric. Default value of this field is "1".
{% set products = products.relatedTo(category).orderBy('productOrderInCategory').inReverse() %}

All is fine, when i do this
{% set productList = products.all() %}

When i use pagination, order of products with same "productOrderInCategory" field value randomly changes each time page loads.
{% paginate products.limit(32) as pageInfo, paginatedProducts %}

So, product with larger productOrderInCategory value will always be first, but multiple products with same productOrderInCategory value will be randomly shuffled each time page loads.
How can i fix that?

**Tags:** elementquery


---

## 1 answers

### Answer by username_1 on 2021-07-30. Score: 2 | Accepted answer

If the field you're ordering by is identical across multiple products, the order in which those are returned is undefined (i.e. determined by the database, essentially random). To achieve a consistent sort order, you need to specify a second field to sort by if the first one is the same. For example, you could just sort products with the same productOrderInCategory alphabetically by title:
{% set products = products.relatedTo(category)
                    .orderBy('productOrderInCategory, title DESC')
                    .inReverse() %}

The syntax for sorting by multiple fields is documented here.

> **Comments:**
> **username_0**: Thank you, that helped.  

---

