In Advanced PDF/HTML templates, item lines normally appear in the same order as they do in the item sublist. However, by using FreeMarker, we have the option to sort the item lines based on any specific column from the sublist.
Sample code: This sorts the item lines based on the location.
<#list record.item?sort_by("location") as item>
...
...
</#list>