[JAVA] Why can't FreeMarker's? Sort build-in work with List <String>?

Why did you come across this situation

I was writing a JBake template in FTL. Nobody uses JBake, right? Well, that's good, but there is information such as a list of all the tags used in the articles on the site, and it seems that this is arranged in the order in which JBake crawls the article data to be processed and finds it. is. The order is out of order.

If you put it out as it is, it will look bad. It doesn't matter what order you use, so you should arrange them in a regular manner. That's why I checked the FTL build-in.

Eh, why is this an error?

That's why I look at the FreeMarker site. Then there is the desired build-in.

TEMPLATE


<#assign ls = ["whale", "Barbara", "zeppelin", "aardvark", "beetroot"]?sort>
<#list ls as i>${i} </#list>

OUTPUT


aardvark Barbara beetroot whale zeppelin

I'll sort numbers, strings, dates, and Boolean values, and there's also? Reverse (free translation).

Oh yeah, that's fine. FTL has a lot of unknown Nanika hidden in it, so be alert. Can someone translate the document? I can't read it in English, but my eyes slip.

That's why I think that it is a speed solution, so I write the following description and bake (in JBake, the conversion / generation process of site content is called Bake).

TEMPLATE


<#--Sort and output(?just added sort) -->
<#list alltags?sort as tag>
  <span>${tag}</tag>
</#list>

Then it's an unexpected exception message, wife.

Failed to render file. Cause:
freemarker.core.NonSequenceException:
For "?sort" left-hand operand:
Expected a sequence, but this has evaluated to a collection
(Appropriately abbreviated)

In short, you say, "It's not Sequence, can't you use sort? (Free translation)". I don't know why it doesn't work in Collection even though sorting is implemented! ??

Well this way it will work

<#--Put it back in Sequences-->
<#assign seq=[]/>
<#list alltags as tag>
  <#assign seq=seq+[tag]/>
</#list>
<#--Sort and output-->
<#list seq?sort as tag>
  <span>${tag}</tag>
</#list>

I only turned lists and array elements with <#list>, so I didn't think there was such a difference between Sequence and Collection. I mean? Sort, I think it's a rough implementation.

Recommended Posts

Why can't FreeMarker's? Sort build-in work with List <String>?
Work list