cancel
Showing results for 
Search instead for 
Did you mean: 

order a section list

michaelc
Champ on-the-rise
Champ on-the-rise
This should be simple.
I am pulling back a list of sections
   model.sections = context.properties.section.sections;

then showing them.
<#list sections as section> 
     <a href="${url.context}${section.path}">${section.name}</a>
</#list>    


but I want them in index order, seem I should have a method for this.
or is it a case I must order in the Javascript.
4 REPLIES 4

rjohnson
Star Contributor
Star Contributor
<#list sections?sort_by("index") as section>

Assuming that your sections structure is [{"index":"index_text", "name":"name_text"}]

Bob Johnson

michaelc
Champ on-the-rise
Champ on-the-rise
Humm.
I tried
   <#list sections?sort_by("orderIndex")  as section>
and
   <#list sections?sort_by("wsSmiley SurprisedrderIndex")  as section>

when I dump the values in section I see.
ws:excludeFromNavigation
cm:modified
cmisSmiley SurprisedbjectTypeId
cmis:name
wsSmiley TonguearentId
cm:name
type
sys:locale
ws:inheritRenditionConfig
id
cm:description
wsSmiley SurprisedrderIndex
cm:creator
cm:created
cmisSmiley SurprisedbjectId
cm:title
cm:modifier
cmis:lastModificationDate

rjohnson
Star Contributor
Star Contributor
I see the difficulty. The only defined sort in FreeMarker (that I am aware of) assumes the input array is a JSON structure and yours isn't. At which point you would have to sort sections in Javascript before passing it through to FreeMarker which is simple enough you just need the function to compare the property wsSmiley SurprisedrderIndex.

so it would be something like


function sortSections(s1, s2) {
   return (s1.properties["ws:orderIndex"] > s2.properties["ws:orderIndex"]) ? 1 : (s1.properties["ws:orderIndex"] < s2.properties["ws:orderIndex"]) ? -1 : 0;
}
sections.sort(sortSections);


Bob Johnson

michaelc
Champ on-the-rise
Champ on-the-rise
So based on what you stated I tried.

function sortSections(s1, s2) {
   return (s1.properties["ws:orderIndex"] > s2.properties["ws:orderIndex"]) ? 1 : (s1.properties["ws:orderIndex"] < s2.properties["ws:orderIndex"]) ? -1 : 0;
}

   var sections = context.properties.section.sections;  // Pull the child sections and passthem down.
   sections.sort(sortSections);

Failed as sort is undefined.

This does not seem to be Javascript as I know it ( see next note )

but based on that the following did work.

function bubbleSort(a)
{
    var swapped;
    do {
        swapped = false;
        for (var i=0; i < a.length-1; i++) {
            if (a > a[i+1]) {
                var temp = a;
                a = a[i+1];
                a[i+1] = temp;
                swapped = true;
            }
        }
    } while (swapped);
}

   var sections = context.properties.section.sections;  // Pull the child sections and passthem down.
   bubbleSort(a);