I've encountered a similar problem as in this post: How to order numbers naturally?

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?

  • 1
    The only way to extend the stock Twig methods (or create a custom method) is to write your own plugin – why not just use SuperSort (referenced in the answer you linked to), which already handles this sort of thing beautifully? Commented Nov 9, 2016 at 23:04
  • 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
    Commented Nov 10, 2016 at 13:22

1 Answer 1


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') %}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.