<p>It's an extra plugin but SuperSort would do the job - <a href="https://github.com/TopShelfCraft/SuperSort" rel="nofollow noreferrer">https://github.com/TopShelfCraft/SuperSort</a></p>

<p>Edit - couldn't make it work in one go, but this works:
<code>
{% 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) %}
</code></p>

<p>Not the most elegant, let me know if you find a better solution!</p>

CC BY-SA 4.0
2019-09-23T09:42:47.697Z
Thanks, Seb - it isn't that clear how we would use the plugin in this scenario though.
Dan Lee
CC BY-SA 4.0
2019-09-23T10:50:10.787Z
Would the following be correct?

{% for field in submission | supersort('sortAs', 'submission[field.handle].value', SORT_NUMERIC) %}
Dan Lee
CC BY-SA 4.0
2019-09-23T20:35:35.013Z
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
CC BY-SA 4.0
2019-09-23T21:06:58.197Z
actually this doesn't work, just edited my answer above with another solution
Seb
CC BY-SA 4.0
2019-09-24T08:04:51.293Z
Thanks - I get an "Array to string conversion" error when adding this line: {% set sortedResults = results | supersort('sortAs', '{value}', SORT_NUMERIC) %}
Dan Lee
CC BY-SA 4.0
2019-09-24T08:38:56.517Z
can you paste the whole code excerpt?
Seb
CC BY-SA 4.0
2019-09-24T08:42:14.373Z
Let us [continue this discussion in chat](https://chat.stackexchange.com/rooms/99032/discussion-between-dan-lee-and-seb).
Dan Lee
CC BY-SA 4.0
2019-09-23T09:01:00.170Z
2019-09-24T07:15:24.923Z
2019-09-24T07:15:24.923Z
Seb
<p>I’m looping through some text fields on the form results page and need to order the numerical output from low to high.</p>

<p><strong>How would I do this using twig?</strong></p>

<pre><code>{% for field in submission %}
  {% submission[field.handle].value %}
{% endfor %}
</code></pre>

<p>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.</p>

<forloop><loop>
Ordering Looped Results by Field's Numerical Value
# Ordering Looped Results by Field's Numerical Value

(Asked by: username_0 on 2019-09-23)

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.

**Tags:** forloop, loop


---

## 1 answers

### Answer by username_1 on 2019-09-23. Score: 1 | Accepted answer

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!

> **Comments:**
> **username_0**: Thanks, username_1 - it isn't that clear how we would use the plugin in this scenario though.  
> **username_0**: Would the following be correct?  {% for field in submission | supersort('sortAs', 'submission[field.handle].value', SORT_NUMERIC) %}  
> **username_1**: 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) %}`  
> **username_1**: actually this doesn't work, just edited my answer above with another solution  
> **username_0**: Thanks - I get an "Array to string conversion" error when adding this line: {% set sortedResults = results | supersort('sortAs', '{value}', SORT_NUMERIC) %}  
> **username_1**: can you paste the whole code excerpt?  
> **username_0**: Let us [continue this discussion in chat](https://chat.stackexchange.com/rooms/99032/discussion-between-dan-lee-and-seb).  

---

