The easiest way would be to sort the data before it {{#each ...}} into the Handlebars, then you can use {{#each ...}} as usual, and no helpers are needed. This approach is quite common with Handlebars, the template is often divided into two parts: a piece of (Java | Coffee) Script for processing / rearranging data and the template itself.
As an aside, you want to set your comparator function to the behavior of the property. From the excellent guide :
If compareFunction supplied, the elements of the array are sorted according to the return value of the comparison function. If a and b compare two elements, then:
- If
compareFunction(a, b) less than 0, sort a with a lower index than b , i.e. at the first place. - If
compareFunction(a, b) returns 0, leave a and b unchanged relative to each other, but sorted by all the different elements. Note. The ECMAscript standard does not guarantee this behavior, and therefore not all browsers (for example, versions of Mozilla dated at least 2003) respect this. - If
compareFunction(a, b) greater than 0, sort b with a lower index than a .
So you want to return 0 if a.sort_index and b.sort_index same, something like this:
myArray.sort (a,b)-> a = +a.sort_index b = +b.sort_index return 1 if a > b return 0 if a == b return -1 if a < b
If you need to perform sorting inside the template, you need to add a special each_with_sort helper for sorting and iteration, for example:
and your template will look like this:
{{#each_with_sort array "sort_index"}} ... {{/each_with_sort}}
Demo: http://jsfiddle.net/ambiguous/zusq2tt4/
source share