I need to sort a list of records and render them in a table, but they have to be sorted by a "From Date" column, and then by "To Date" at a second level. I checked the documentation for ?sort_by but it doesn’t mention any support for double sorting.
Thought these would work but it’s doing a different thing.
<#list invoices?sort_by(['from_date', 'to_date']) as inv>
<#list invoices?sort_by("from_date")?sort_by("to_date") as inv>
Also, some records have empty values for "From Date" or "To Date".
2
Answers
I ended up doing something similar to this question, but had to add some parsing and render the records with empty dates at the bottom of the table.
An adaptation of my Freemarker code:
Can be tested in https://try.freemarker.apache.org/ changing this line:
to
Data Model:
Result:
Dealing with the empty values is another matter but Freemarker uses the sort from java.utils.Collection which is stable (i.e. multi-key sorts retain the original order for equal keys)
So:
Will get you what you are asking for.
What this does is first sort by
to_date
. Then re-sort byfrom_date
and any invoices where thefrom_date
values are equal will retain their stableto_date
sort order.