<p>It's possible to pass MySQL functions to the <code>order</code> parameter. <a href="https://www.copterlabs.com/natural-sorting-in-mysql/" rel="nofollow noreferrer">Using the MySQL <code>LENGTH</code> function</a>, I believe the following should do the trick <em>(assuming your "model number" field's handle is <code>modelNumber</code>)</em>:</p>

<pre><code>{% set entries = craft.entries.section('yourSection').order('LENGTH(modelNumber) asc, modelNumber asc') %}
</code></pre>

CC BY-SA 3.0
2016-11-10T19:55:03.973Z
2016-11-10T20:22:47.473Z
2016-11-10T20:22:47.473Z
Mats Mikkel Rummelhoff
<p>I've encountered a similar problem as in this post: <a href="https://craftcms.stackexchange.com/q/8931/4468">How to order numbers naturally?</a></p>

<p>I have some products I need sorted on a page by model number, and currently they sort as ABC-1, ABC-12, ABC-2, etc. instead of ABC-1, ABC-2, ABC-12. Is it possible to extend the existing methods or create a custom sort method to use with the order parameter, rather than using a twig filter as suggested in the link above?</p>

CC BY-SA 3.0
2016-11-09T23:04:47.437Z
The only way to extend the stock Twig methods (or create a custom method) is to [write your own plugin](https://cameronspear.com/blog/craft-cms-twig-filter-extension/) – why not just use [SuperSort](https://github.com/TopShelfCraft/SuperSort) (referenced in the answer you linked to), which already handles this sort of thing beautifully?
Mats Mikkel Rummelhoff
CC BY-SA 3.0
2016-11-10T13:22:22.033Z
The page I'm working on has a dropdown that users can select sort order from. The corresponding sort method is then passed into .order() and the page is updated. There are certain scenarios that I'm not sure the plugins will work as they use Twig filters. For example, one method sorts between items flagged as new products and those that aren't, then within those two segments sorts them alphabetically by product number. I've updated my original question to reflect that I'm trying to avoid Twig filters.
Joel
<order><sort>
Extend order methods to sort numbers naturally
# Extend order methods to sort numbers naturally

(Asked by: username_0 on 2016-11-09)

I've encountered a similar problem as in this post: [How to order numbers naturally?](https://craftcms.stackexchange.com/q/8931/4468)

I have some products I need sorted on a page by model number, and currently they sort as ABC-1, ABC-12, ABC-2, etc. instead of ABC-1, ABC-2, ABC-12. Is it possible to extend the existing methods or create a custom sort method to use with the order parameter, rather than using a twig filter as suggested in the link above?

**Tags:** order, sort
> **Comments:**
> **username_1**: The only way to extend the stock Twig methods (or create a custom method) is to [write your own plugin](https://cameronspear.com/blog/craft-cms-twig-filter-extension/) – why not just use [SuperSort](https://github.com/TopShelfCraft/SuperSort) (referenced in the answer you linked to), which already handles this sort of thing beautifully?  
> **username_0**: The page I'm working on has a dropdown that users can select sort order from. The corresponding sort method is then passed into .order() and the page is updated. There are certain scenarios that I'm not sure the plugins will work as they use Twig filters. For example, one method sorts between items flagged as new products and those that aren't, then within those two segments sorts them alphabetically by product number. I've updated my original question to reflect that I'm trying to avoid Twig filters.  

---

## 1 answers

### Answer by username_1 on 2016-11-10. Score: 1 | Accepted answer

It's possible to pass MySQL functions to the order parameter. Using the MySQL LENGTH function, I believe the following should do the trick (assuming your "model number" field's handle is modelNumber):
{% set entries = craft.entries.section('yourSection').order('LENGTH(modelNumber) asc, modelNumber asc') %}


---

