This function returns a result sequence from sorting a sequence of Hash-type objects.
Sorting can be in ascending or descending order. It is performed according to the values of an object attribute.
To do this, the attribute name or sequence of attribute names representing the path in dot notation leading to the sub-level object attribute needs to be specified.
When the indicated attribute values are String type, lexicographic sorting is applied.
NB:
Some differences in relation to the FreeMarker "built-in" "sort_by" function (click here for more details) applied to a ([...]?sort_by) sequence:
-
- The obtained result is identical for Boolean/Number/Date/Time/Date-time types. For the String type, the FreeMarker "sort_by" function applies a sort using a collation,
- The "sort_by" function does not sort in descending order. The only solution is to use the FreeMarker "reverse" ([...]?sort_by()?reverse) function, but the original order of equal values is not respected.
Parameters
Hash sequence |
seqHash |
Hash object sequence to sort |
Mandatory |
String/String Sequence |
sedAttrName |
Object attribute on which to apply the sort. It is identified by its name or attribute name sequence representing the path in dot notation leading to it. |
Mandatory |
Boolean |
ascendingOrder |
"true" for an ascending sort, "false" for a descending sort |
Optional. The default value is "true" |
For example
<#assign seqObj = [ {"name":"whale", "weight":2000}, {"name":"Barbara", "weight":53}, {"name":"zeppelin", "weight":-200}, {"name":"aardvark", "weight":30}, {"name":"beetroot", "weight":0.3} ]> <#-- Order by name --> <#list hardisCore.lexicographicSortBy(seqObj, "name") as aObj> - ${aObj.name}: ${aObj.weight} </#list>
The output is:
- aardvark: 30
- Barbara: 53
- beetroot: 0.3
- whale: 2000
- zeppelin: -200
Another example
<#assign members = [ {"name": {"first": "Joe", "last": "Smith"}, "age": 40}, {"name": {"first": "Fred", "last": "Crooger"}, "age": 35}, {"name": {"first": "Amanda", "last": "Fox"}, "age": 25}]> <#-- Sorted by name.last --> <#list hardisCode.lexicographicSortBy(members, ['name', 'last']) as aObj> - ${aObj.name.last}, ${aObj.name.first}: ${aObj.age} years old </#list> ?
The output is:
- Crooger, Fred: 35 years old
- Fox, Amanda: 25 years old
- Smith, Joe: 40 years old
↑ Top of page