I’m looping through some text fields on the form results page and need to order the numerical output from low to high.

How would I do this using twig?

{% for field in submission %}
  {% submission[field.handle].value %}
{% endfor %}

Note: I am using the Freeform Plugin. They have informed me that I would need to create a custom filter but before I do that wanted to see if there was a better way to do this.


It's an extra plugin but SuperSort would do the job - https://github.com/TopShelfCraft/SuperSort

Edit - couldn't make it work in one go, but this works: {% set results = [] %} {% for field in submission %} {% set results = results | merge([{'handle': field.handle, 'value': submission[field.handle].value}]) %} {% endfor %} {% set sortedResults = results | supersort('sortAs', '{value}', SORT_NUMERIC) %}

Not the most elegant, let me know if you find a better solution!

  • Thanks, Seb - it isn't that clear how we would use the plugin in this scenario though. – Dan Lee Sep 23 at 9:42
  • Would the following be correct? {% for field in submission | supersort('sortAs', 'submission[field.handle].value', SORT_NUMERIC) %} – Dan Lee Sep 23 at 10:50
  • yes though I believe you need to use the twig delimiters, and use object (untested): {% set sortedSubmission = submission | supersort('sortAs', '{{ submission[object.handle].value }}', SORT_NUMERIC) %} – Seb Sep 23 at 20:35
  • actually this doesn't work, just edited my answer above with another solution – Seb Sep 23 at 21:06
  • Thanks - I get an "Array to string conversion" error when adding this line: {% set sortedResults = results | supersort('sortAs', '{value}', SORT_NUMERIC) %} – Dan Lee Sep 24 at 8:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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