<p>You can fake it by first sorting by character length, and then by the value: <code>$criteria-&gt;order = 'CHAR_LENGTH(myField) asc, myField asc</code></p>

<p>Its not perfect, but in certain cases, it will be all you need. Other methods exist as well: <a href="https://stackoverflow.com/a/8557307/935398">https://stackoverflow.com/a/8557307/935398</a></p>

<p>Note: <code>CHAR_LENGTH</code> is a MySQL function that is getting passed directly to the <code>ORDER BY</code> clause. It has nothing to do with Craft directly.</p>

CC BY-SA 3.0
2016-06-01T14:37:10.663Z
2016-06-01T14:44:37.670Z
2017-05-23T12:38:53.957Z
Tim Kelty
<p>I want something sorted like this:</p>

<ul>
<li>MyString8</li>
<li>MyString9</li>
<li>MyString10</li>
<li>MyString11</li>
</ul>

<p><code>$criteria-&gt;order = 'myField asc</code> doesn't get things quite right, as MySQL has not native "natural sorting".</p>

<elementcriteriamodel><sorting>
"Natural" sorting for ElementCriteria
# "Natural" sorting for ElementCriteria

(Asked by: username_0 on 2016-06-01)

I want something sorted like this:

* MyString8
* MyString9
* MyString10
* MyString11

`$criteria->order = 'myField asc` doesn't get things quite right, as MySQL has not native "natural sorting".

**Tags:** elementcriteriamodel, sorting


---

## 1 answers

### Answer by username_0 on 2016-06-01. Score: 2

You can fake it by first sorting by character length, and then by the value: `$criteria->order = 'CHAR_LENGTH(myField) asc, myField asc`

Its not perfect, but in certain cases, it will be all you need. Other methods exist as well: <https://stackoverflow.com/a/8557307/935398>

Note: `CHAR_LENGTH` is a MySQL function that is getting passed directly to the `ORDER BY` clause. It has nothing to do with Craft directly.


---

