Sort by nested field value in FTL liferay

1.5k views Asked by At

My Structure created in liferay is as shown below:

{
    "availableLanguageIds": [
        "en_US"
    ],
    "defaultLanguageId": "en_US",
    "fields": [
        {
            "label": {
                "en_US": "Image"
            },
            "predefinedValue": {
                "en_US": ""
            },
            "style": {
                "en_US": ""
            },
            "tip": {
                "en_US": "Upload the image to be displayed in the Small Banner"
            },
            "dataType": "image",
            "fieldNamespace": "ddm",
            "indexType": "keyword",
            "localizable": true,
            "name": "smallImage",
            "readOnly": false,
            "repeatable": true,
            "required": true,
            "showLabel": true,
            "type": "ddm-image",
            "nestedFields": [
                {
                    "label": {
                        "en_US": "Priority of Image"
                    },
                    "predefinedValue": {
                        "en_US": "0"
                    },
                    "style": {
                        "en_US": ""
                    },
                    "tip": {
                        "en_US": "Priority of the uploaded Image. Lower the number, higher the priority."
                    },
                    "dataType": "integer",
                    "fieldNamespace": "ddm",
                    "indexType": "keyword",
                    "localizable": true,
                    "name": "imagePriority",
                    "readOnly": false,
                    "repeatable": false,
                    "required": true,
                    "showLabel": true,
                    "type": "ddm-integer"
                }
            ]
        }
    ]
}

Now in the template I want to sort it using the priority field of the image. My template is as shown below:

<div id="example2" class="slider-pro">
    <div class="sp-slides">
        <#if smallImage.getSiblings()?has_content>
            <#list smallImage.getSiblings()?sort_by(["properties","imagePriority"]) as cur_smallImage>
                <#if cur_smallImage.getData()?? && cur_smallImage.getData() != "">
                    <div class="sp-slide">
                        <img class="sp-image" alt="${cur_smallImage.getAttribute("alt")}" src="${cur_smallImage.getData()}" data-src="${cur_smallImage.getData()}" data-retina="${cur_smallImage.getData()}" />
                    </div>
                </#if>
            </#list>
        </#if>
    </div>
</div>

I tried the sorting as ?sort_by("cur_smallImage.imagePriority") also by ?sort_by("imagePriority") but it didnt work. I want to display the images in the sequence of the priority specified.
Please help.

1

There are 1 answers

0
Dror On

If the subvariable that you want to use for the sorting is on a deeper level (that is, if it is a subvariable of a subvariable and so on), then you can use a sequence as parameter, that specifies the names of the sub variables that lead down to the desired subvariable. For 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 members?sort_by(['name', 'last']) as m>
- ${m.name.last}, ${m.name.first}: ${m.age} years old
</#list>

taken from: https://freemarker.apache.org/docs/ref_builtins_sequence.html#ref_builtin_sort_by