# Velocity #foreach on a sorted foundset

**URL:** <https://forum.servoy.com/t/velocity-foreach-on-a-sorted-foundset/20556>\
**Category:** Questions\
**Created:** [August 6, 2021, 5:02am UTC](https://forum.servoy.com/t/velocity-foreach-on-a-sorted-foundset/20556 "2021-08-06T05:02:47Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![roddy](https://avatars.discourse-cdn.com/v4/letter/r/a5b964/32.png) [@roddy](https://forum.servoy.com/u/roddy)\
**Post date:** [August 6, 2021, 5:02am UTC](https://forum.servoy.com/t/velocity-foreach-on-a-sorted-foundset/20556/1 "2021-08-06T05:02:47Z")

</div>

I am sorting a foundset and writing these to a related foundset that will be used in the printed Velocity. The sort order of the related foundset is getting lost in the report.

It is quite a complex sorting algorithm; I could add a simpler sort column in the printing foundset but I cannot figure out how to get the #foreach in the Velocity template to utilise a sorted order.

Any help would be much appreciated, thanks.

---

<div class="post-metadata">

**Author:** ![roddy](https://avatars.discourse-cdn.com/v4/letter/r/a5b964/32.png) [@roddy](https://forum.servoy.com/u/roddy)\
**Post date:** [August 6, 2021, 7:25am UTC](https://forum.servoy.com/t/velocity-foreach-on-a-sorted-foundset/20556/2 "2021-08-06T07:25:49Z")

</div>

For anyone else finding this; I had to sort the original foundset; then I looped through each to write it to the foundset for the printing with the index as a sort\_order column in the printing foundset. Once saved, I sort the printing foundset using the sort\_order column and this then renders in the correct order.
