Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-06-12T14:39:40.727Z
Answers.LastActivityDate: 2014-06-12T14:39:40.727Z
Answers.UserDisplayName: Victor
Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-06-12T15:04:54.820Z
Answers.Text: 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?
Answers.UserDisplayName: carlcs
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-06-12T15:07:45.303Z
Answers.Text: See http://buildwithcraft.com/docs/sections-and-entries#dynamic-entry-titles
Answers.UserDisplayName: Brandon Kelly
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-10-28T22:24:29.437Z
Answers.Text: 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)
Answers.UserDisplayName: Brimby
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-06-12T14:40:11.363Z
Answers.LastActivityDate: 2014-06-12T15:07:30.360Z
Answers.LastEditDate: 2014-06-12T15:07:30.360Z
Answers.UserDisplayName: Brandon Kelly
Body: <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>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2014-06-12T13:55:11.920Z
Comments.Text: 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)
Comments.UserDisplayName: Nicolaides
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2014-06-12T13:59:34.810Z
Comments.Text: 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.
Comments.UserDisplayName: Spoeken
Tags: <sorting><templating>
Title: How can I order entries by title when each title is numeric?
ThreadText: # 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](https://stackoverflow.com/questions/21882510/craft-order-by-title-that-is-a-number), 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() %}
      <h2>§{{entry.title}}</h2>
  {% 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](http://dev.mysql.com/doc/refman/5.0/en/cast-functions.html#function_cast):
  
  ```
  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](http://buildwithcraft.com/docs/sections-and-entries#dynamic-entry-titles) 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/username_2-in/Craft-TwigBetterSort>
  
  
  ---
  
  