<p>Twig doesn't have a <code>natsort()</code> filter.</p>

<p>But I've put together a simple plugin which extends the sort-filter:</p>

<p><a href="https://github.com/victor-in/Craft-TwigBetterSort" rel="nofollow">https://github.com/victor-in/Craft-TwigBetterSort</a></p>

CC BY-SA 3.0
2014-06-12T14:39:40.727Z
2014-06-12T14:39:40.727Z
Victor
<p>You can do that using MySQL’s <a href="http://dev.mysql.com/doc/refman/5.0/en/cast-functions.html#function_cast">cast() function</a>:</p>

<pre><code>craft.entries.section('mySection').order('cast(title as unsigned)')
</code></pre>

<p>Alternatively, you might get better performance if you create an actual Number field called “numberTitle”, attach it to your Entry Type, and set the Entry Type’s title to be <a href="http://buildwithcraft.com/docs/sections-and-entries#dynamic-entry-titles">dynamically generated</a> using the template “<code>{numberTitle}</code>”. Then you can just order by that field instead:</p>

<pre><code>craft.entries.section('mySection').order('numberTitle')
</code></pre>

CC BY-SA 3.0
2014-06-12T15:04:54.820Z
That 2nd technique is interesting, but where exactly do you set that template for the title field? Can I set templates for other fields too?
carlcs
CC BY-SA 3.0
2014-06-12T15:07:45.303Z
See http://buildwithcraft.com/docs/sections-and-entries#dynamic-entry-titles
Brandon Kelly
CC BY-SA 4.0
2020-10-28T22:24:29.437Z
Amazing answer. Just watch out if you're ordering by a custom field you need to put `field_` in front of the field handle. Eg: `craft.entries().section('mySection').orderBy('cast(field_customFieldHandle as unsigned)')` (Also use `orderBy` instead of `order` in Craft 3)
Brimby
CC BY-SA 3.0
2014-06-12T14:40:11.363Z
2014-06-12T15:07:30.360Z
2014-06-12T15:07:30.360Z
Brandon Kelly
<p><em><a href="https://stackoverflow.com/questions/21882510/craft-order-by-title-that-is-a-number">I originally asked this on Stack Overflow</a>, but I'm hoping for more response here.</em></p>

<p>In Craft, I'm trying to order entries by the title. Each title is a number, but the sort function handles it like a string. Because of that <code>100</code> shows up before <code>2</code>, <code>31</code> before <code>4</code>, and so on.</p>

<pre><code>{% for entry in craft.entries.section('mySection').order('title').find() %}
    &lt;h2&gt;§{{entry.title}}&lt;/h2&gt;
{% endfor %}
</code></pre>

<p>Is there a way to do this without extending  twig?</p>

CC BY-SA 3.0
2014-06-12T13:55:11.920Z
I see that you've ask this question before, over on main stackoverflow. Did you try the answer there using **natsort**?  [Question with natsort answer](http://stackoverflow.com/questions/21882510/craft-order-by-title-that-is-a-number)
Nicolaides
CC BY-SA 3.0
2014-06-12T13:59:34.810Z
Yes, as mentioned in my question I originally asked this on stack overflow. I would like to know is if there is a way to do this without extending twig.
Spoeken
<sorting><templating>
How can I order entries by title when each title is numeric?
# How can I order entries by title when each title is numeric?

(Asked by: username_0 on 2014-06-12)

I originally asked this on Stack Overflow, but I'm hoping for more response here.
In Craft, I'm trying to order entries by the title. Each title is a number, but the sort function handles it like a string. Because of that 100 shows up before 2, 31 before 4, and so on.
{% for entry in craft.entries.section('mySection').order('title').find() %}
    §{{entry.title}}
{% endfor %}

Is there a way to do this without extending  twig?

**Tags:** sorting, templating

> **Comments:**
> **username_1**: I see that you've ask this question before, over on main stackoverflow. Did you try the answer there using **natsort**?  [Question with natsort answer](http://stackoverflow.com/questions/21882510/craft-order-by-title-that-is-a-number)  
> **username_0**: Yes, as mentioned in my question I originally asked this on stack overflow. I would like to know is if there is a way to do this without extending twig.  

---

## 2 answers

### Answer by username_3 on 2014-06-12. Score: 14 | Accepted answer

You can do that using MySQL’s cast() function:
craft.entries.section('mySection').order('cast(title as unsigned)')

Alternatively, you might get better performance if you create an actual Number field called “numberTitle”, attach it to your Entry Type, and set the Entry Type’s title to be dynamically generated using the template “{numberTitle}”. Then you can just order by that field instead:
craft.entries.section('mySection').order('numberTitle')

> **Comments:**
> **username_4**: That 2nd technique is interesting, but where exactly do you set that template for the title field? Can I set templates for other fields too?  
> **username_3**: See http://buildwithcraft.com/docs/sections-and-entries#dynamic-entry-titles  
> **username_5**: Amazing answer. Just watch out if you're ordering by a custom field you need to put `field_` in front of the field handle. Eg: `craft.entries().section('mySection').orderBy('cast(field_customFieldHandle as unsigned)')` (Also use `orderBy` instead of `order` in Craft 3)  

---

### Answer by username_2 on 2014-06-12. Score: 4

Twig doesn't have a `natsort()` filter.

But I've put together a simple plugin which extends the sort-filter:

<https://github.com/victor-in/Craft-TwigBetterSort>


---

