<p>It's not a good idea to express the sorting logic in PHP, since that requires loading <em>all</em> results and then running them through the sorting function. If you have 10,000 entries and need the first 20 (according to your sort function), you're still going to load 10,000 entries and then throw out 9,980 of them.</p>
<p>Instead, you need to specify the sorting logic in the entry query, so the database can return the results in the correct sort order, and only return the number of results you need.</p>
<p>The <a href="https://docs.craftcms.com/api/v4/craft-elements-db-elementquery.html#method-orderby" rel="nofollow noreferrer"><code>orderBy</code> method</a> can take multiple forms. You can specify a single field or multiple fields in array notation, as well as the sort order (ascending or descending) for every field.</p>
<p>If that is not enough, you can use an <a href="https://www.yiiframework.com/doc/api/2.0/yii-db-expressioninterface" rel="nofollow noreferrer">expression</a> to specify a custom sort function in plain SQL. You can create an expression in Twig using the <a href="https://craftcms.com/docs/4.x/dev/functions.html#expression" rel="nofollow noreferrer"><code>expression</code> function</a>:</p>
<pre class="lang-twig prettyprint-override"><code>{% set result = craft.entries()
    .section('somename')
    .orderBy(expression('your expression here'))
    .all()
%}
</code></pre>

CC BY-SA 4.0
2023-01-19T11:42:36.633Z
2023-01-19T11:42:36.633Z
MoritzLost
<p>Is there a way to sort entries by a customSortOrderFunction in a plugin?</p>
<p>Like: <code>craft.entries.section('somename').orderBy(myCustomSortOrderFunction)</code></p>
<p>Where <code>myCustomSortOrderFunction</code> returns some data from another table.</p>
<p>Any suggestion or article, etc. will be helpful.</p>
<p>Thanks.</p>

<craft4><sorting>
Craft CMS 4 custom orderBy
# Craft CMS 4 custom orderBy

(Asked by: username_0 on 2023-01-19)

Is there a way to sort entries by a customSortOrderFunction in a plugin?

Like: `craft.entries.section('somename').orderBy(myCustomSortOrderFunction)`

Where `myCustomSortOrderFunction` returns some data from another table.

Any suggestion or article, etc. will be helpful.

Thanks.

**Tags:** craft4, sorting

---

## 1 answers

### Answer by username_1 on 2023-01-19. Score: 1

It's not a good idea to express the sorting logic in PHP, since that requires loading all results and then running them through the sorting function. If you have 10,000 entries and need the first 20 (according to your sort function), you're still going to load 10,000 entries and then throw out 9,980 of them.
Instead, you need to specify the sorting logic in the entry query, so the database can return the results in the correct sort order, and only return the number of results you need.
The orderBy method can take multiple forms. You can specify a single field or multiple fields in array notation, as well as the sort order (ascending or descending) for every field.
If that is not enough, you can use an expression to specify a custom sort function in plain SQL. You can create an expression in Twig using the expression function:
{% set result = craft.entries()
    .section('somename')
    .orderBy(expression('your expression here'))
    .all()
%}


---

