# Specific sort

**URL:** <https://forum.ansible.com/t/specific-sort/28392>\
**Category:** Ansible Project\
**Created:** [August 22, 2018, 9:24am UTC](https://forum.ansible.com/t/specific-sort/28392 "2018-08-22T09:24:28Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jean-yves](https://avatars.discourse-cdn.com/v4/letter/j/a587f6/32.png) [@jean-yves](https://forum.ansible.com/u/jean-yves)\
**Post date:** [August 22, 2018, 9:24am UTC](https://forum.ansible.com/t/specific-sort/28392/1 "2018-08-22T09:24:28Z")

</div>

Hi,

I have some patch to apply in order, so I have to sort my list which came from an RPM...

By default my list is sorted like this :  
/update/v902002.5\_p05.0\_00\_0  
/update/v902002.5\_p05.1\_08\_0  
/update/v902002.5\_p05.1\_08\_1  
/update/v902002.5\_p05.1\_08\_10  
/update/v902002.5\_p05.1\_08\_2  
/update/v902002.5\_p05.1\_08\_3  
/update/v902002.5\_p05.1\_08\_4  
/update/v902002.5\_p05.1\_08\_5  
/update/v902002.5\_p05.1\_08\_6  
/update/v902002.5\_p05.1\_08\_7  
/update/v902002.5\_p05.1\_08\_8  
/update/v902002.5\_p05.1\_08\_9

I would love an ansible way to do sort with perhaps a specific option to do like the linux sort with option -V which give me this :

/update/v902002.5\_p05.0\_00\_0  
/update/v902002.5\_p05.1\_08\_0  
/update/v902002.5\_p05.1\_08\_1  
/update/v902002.5\_p05.1\_08\_2  
/update/v902002.5\_p05.1\_08\_3  
/update/v902002.5\_p05.1\_08\_4  
/update/v902002.5\_p05.1\_08\_5  
/update/v902002.5\_p05.1\_08\_6  
/update/v902002.5\_p05.1\_08\_7  
/update/v902002.5\_p05.1\_08\_8  
/update/v902002.5\_p05.1\_08\_9  
/update/v902002.5\_p05.1\_08\_10

Is there's such an option of sort available ?  
If not, is there a way to achieve it ?

Thanks

Regards,

JYL

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [August 22, 2018, 3:00pm UTC](https://forum.ansible.com/t/specific-sort/28392/2 "2018-08-22T15:00:27Z")

</div>

Currently we have [http://jinja.pocoo.org/docs/2.10/templates/#sort](http://jinja.pocoo.org/docs/2.10/templates/#sort),  
which does not seem to support a 'numeric' option. A custom filter  
would be required.

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [August 22, 2018, 4:39pm UTC](https://forum.ansible.com/t/specific-sort/28392/3 "2018-08-22T16:39:41Z")

</div>

Elaborating a bit, see [https://natsort.readthedocs.io/en/master/index.html](https://natsort.readthedocs.io/en/master/index.html).

First "pip install natsort".  
Then create filters.py:

from jinja2.\_compat import string\_types  
# Custom filter for use with Jinja2  
def natsort(arg):  
&nbsp;&nbsp;&nbsp;&nbsp;from natsort import natsorted  
&nbsp;&nbsp;&nbsp;&nbsp;return natsorted(arg)  
class FilterModule(object):  
&nbsp;&nbsp;&nbsp;&nbsp;def filters(self):  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'natsort': natsort  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}

and store it in your filters path (see 'ansible-config dump | grep  
DEFAULT\_FILTER\_PLUGIN\_PATH').

Now you can just use "{{ my\_version|natsort }}" which gives the order  
you're looking for.

Dick
