我正在使用FreeMarker模板创建使用HighChart的图表输出,我们需要根据2个不同的列进行双重排序。
示例:使用2个不同的列排序国家/地区列表"区域"和#34;国家"
我检查了其他论坛,发现已在数组元素中定义了双重排序,如下所示,但不是在单个元素级别。
current.children?sort_by('type')?sort_by(['properties','cm:name'])
以下是代码段
<#--Assign the array values-->
<#assign country = country + [ {
"category":"${level1}",
"value":level1Value?number,
"color": color?string,
"superRegion":"${level3?upper_case}",
"region":"${level2?upper_case}"
} ] >
对列表进行排序 - 以下双重排序不起作用。有谁知道如何进行这种双重排序?
<#list country?sort_by("superRegion")?reverse?sort_by("region") as countrySorted>
答案 0 :(得分:1)
?sort_by
无法通过复合&#34;键&#34;进行排序。 (截至2.3.23)。应用?sort_by
两次只需重新排序整个列表。
通常,列表应该在将它们传递给模板之前进行排序。我不确定你的情况是否可行。如果没有,你仍然可以为它写一个TemplateMethodModelEx
并将其放入数据模型等。(另外我已经注意到?sort_by
应该能够做到这一点,因为它已经存在了......也许一些贡献者会接受它。)
答案 1 :(得分:0)
您可以覆盖列表:
<#assign mylist = mylist?sort_by("ID") />
<#assign mylist = mylist?sort_by("TYPETRAITEMENT") />
<#assign mylist = mylist?sort_by("DOMAINEMETIER") />
答案 2 :(得分:0)
已解决的情况如下。两次使用sort_by函数将无济于事。我这样解决:列表,过滤器,排序依据。这里是例子。
可以在此处检查:https://try.freemarker.apache.org/
模板:
Sorted by gender:
<#list humans?sort_by("gender") as h>
<#if !(genders??)>
<#assign prevGender = h.gender />
<#assign genders = h.gender + "---" />
</#if>
<#if prevGender != h.gender>
<#assign genders = genders + h.gender + "---" />
</#if>
<#assign prevGender = h.gender />
index: ${h.index}, gender: ${h.gender}, name: ${h.name}, age: ${h.age}.
</#list>
Sorted by gender, name:
<#list genders?split("---") as g>
<#if g?? && g?trim!="">gender: ${g}</#if>
<#list humans?filter(x -> x.gender = g)?sort_by("name") as x>
index: ${x.index}, gender: ${x.gender}, name: ${x.name}, age: ${x.age}.
</#list>
</#list>
数据模型:
humans=[{"index":0,"gender":"male","name":"Bazil","age":34},{"index":1,"gender":"male","name":"Serhio","age":33},{"index":2,"gender":"male","name":"Andrew","age":32},{"index":3,"gender":"male","name":"Arty","age":34},{"index":4,"gender":"male","name":"Alex","age":33},{"index":5,"gender":"female","name":"July","age":30},{"index":6,"gender":"female","name":"Anna","age":31},{"index":7,"gender":"female","name":"Tatiana","age":32}]