freemarker-notifications mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Ondra Žižka (JIRA) <>
Subject [jira] [Created] (FREEMARKER-61) ?sort_using with a Comparator parameter
Date Tue, 11 Jul 2017 13:55:00 GMT
Ondra Žižka created FREEMARKER-61:

             Summary: ?sort_using with a Comparator parameter
                 Key: FREEMARKER-61
             Project: Apache Freemarker
          Issue Type: New Feature
          Components: engine
    Affects Versions: 2.3.26-incubating
            Reporter: Ondra Žižka

Hi Daniel :)

I know that lists should be sorted before passing them to the template in general.
In our case, the template is backed by a generic API for graph database which gives an {{Iterable}}
for any 1:N relation. So sorting needs to happen in the template.

Now I'd like to sort that, for which the {{?sort_by(["...", "..."])}} is good enough, except
when it gets a bit more complicated - e.g. sometimes the values are missing in which case
it should sort by something else...

Doing that in a template is not a good idea, so I suggest this, elegant in my opinion, solution:
First you'd have a Comparator in Java, and perhaps register it like you do with {{FreemarkerMethod}}.
Then you could pass this as a parameter to {{?sort_using.}}

public class SortByName implements Comparator { ... }


This would be even better if it could pass parameters to the constructor:

public class SortByName implements Comparator {
    public SortByName(boolean ladiesFirst) { ... }

myData.persons?sort_using("SortByName", [true])

This would greatly leverage sorting in templates while not complicating the template syntax
or cluttering {{?sort_by}}.

Thanks for considering.

This message was sent by Atlassian JIRA

View raw message