Jinja2 sorting numbers

How can I sort numbers by the numeric value instead of the string value?

{{ [“1”,“2”,“100”,“3”,“200”] | sort }}

they are strings as they have Quotes around them
which make them sort like letters

1 and 100 go together

dont know how turn into a number haven’t read that part of the manual yet

In case others search for an answer, the map function can be used to convert strings into numbers:
{{ [‘1’,‘2’,‘100’,‘3’,‘200’] | map(‘int’) | sort }}

Meanwhile I created a filter called “natsort”.
{{ [‘1%’,‘2%’,‘100%’,‘3%’,‘200%’] | natsort }}
gives 1%, 2%, 3%, 100%, 200%

1 Like