Custom sorting order

lori's Avatar

lori

14 May, 2015 08:53 PM

Is there a way to assign a custom sorting order to my section array? Currently, my list is alphabetical, however I require a more specific order.

#set( $pageTitle = $_XPathTool.selectSingleNode($contentRoot, "//system-page[@current]/title") )
#set ( $sectionTitle = $_XPathTool.selectSingleNode($contentRoot, "//system-page[@current]/system-data-structure/sectionTitle") )

<h1>$_EscapeTool.xml($pageTitle.value)</h1>
    #if($sectionTitle.value != "")
        <h2 class="subtitle">$_EscapeTool.xml($sectionTitle.value)</h2>
    #end
    <hr />


#set( $sections = $_XPathTool.selectNodes($contentRoot, "//system-page/dynamic-metadata[name = 'position']") )
#if ( $sections.size() > 0 )
    $_SortTool.addSortCriterion( "value", "en", "text", "ascending", "upper-first")
    $_SortTool.sort($sections)
    ## Create an array of unique section values.
    #set ( $sectionArr = [] )
    #foreach ( $section in $sections )
        #set ( $value = $section.getChild("value").value )
        #if ( $sectionArr.indexOf($value) == -1 )
            #set ( $_void = $sectionArr.add($value) )
        #end
    #end
    ## Loop over the unique sections and display the section and pages belonging to that section.
    #foreach ( $section in $sectionArr )
        #set($pages = $_XPathTool.selectNodes($contentRoot, "//system-page[name != 'index' and dynamic-metadata[name = 'position' and value = '${section}']]"))
        <h2>${section}</h2>
        #if ( $pages.size() > 0 )
            #foreach ( $page in $pages )
                #set( $name = $page.getChild('title').value )
                #set( $data = $page.getChild('system-data-structure') )
                #set( $title = $data.getChild('title').value )                
                #set( $image = $data.getChild('image').getChild('path').value )
                #set( $contactInfo = $data.getChild('contactInfo') )
                #set( $phone = $contactInfo.getChild('phone').value )
                #set( $office = $contactInfo.getChild('office').value )
                #set( $building = $contactInfo.getChild('building').value )
                #set( $email = $contactInfo.getChild('email').value )
                #set( $topics = $_XPathTool.selectNodes($data, "interests/topic") )
                #set( $areas = $data.getChild('areas').value )
                #set( $courses = $data.getChild('courses').value )
                #set( $publications = $data.getChild('publications').value )
                #set( $website = $page.getChild('link').value )
                <div style="display:block;clear:both;">
                <p>
                    <img alt="${name}" src="${image}" width="150px" class="" />
                    <strong>$name</strong>#if ($title != "")<strong>, $title </strong>#end
                    
                    <br />
                    <strong>Contact:</strong><br />
                    #if ($phone != "")
                        Phone: $phone <br />
                    #end
                    #if ($office != "")
                        Office: $office <br />
                    #end
                    #if ($building != "")
                        Building: $building <br />
                    #end
                    #if ($email != "")
                        Email: <a href="mailto:${email}">$_EscapeTool.xml($email)</a> <br /> 
                    #end
                    #if ($website != "")
                        Profile: <a href="${website}">$_EscapeTool.xml($name)</a><br />
                    #end
                    <br />
                </p>
                </div>

            #end
            <hr />
        #end
        <br />
    #end
#end
  1. 1 Posted by Ryan Griffith on 15 May, 2015 01:33 PM

    Ryan Griffith's Avatar

    Hi Lori,

    If you are trying to further sort your sections, you could add additional sorting criterion for the Sort Tool to use when sorting. To do so, simply add additional sort criterion in the desired order. For example:

    $_SortTool.addSortCriterion( "value", "en", "text", "ascending", "upper-first")
    $_SortTool.addSortCriterion( ... )
    $_SortTool.addSortCriterion( ... )
    $_SortTool.sort($sections)
    

    If you need to sort the pages within the sections, you can use the Sort Tool again within the #foreach loop that is outputting the section. For example, let's say you want to sort the pages in each section by their building:

    ## Loop over the unique sections and display the section and pages belonging to that section.
    #foreach ( $section in $sectionArr )
        #set($pages = $_XPathTool.selectNodes($contentRoot, "//system-page[name != 'index' and dynamic-metadata[name = 'position' and value = '${section}']]"))
        <h2>${section}</h2>
        #if ( $pages.size() > 0 )
            $_SortTool.addSortCriterion( "system-data-structure/contactInfo/building", "en", "text", "ascending", "upper-first")
            $_SortTool.sort($pages)
            #foreach ( $page in $pages )
                ...
            #end
        #end
    #end
    

    Also, I wanted to note that the #foreach you have to build the $sectionArr variable may not be necessary. Instead, I believe you can tweak the XPath used to obtain those dynamic metadata values to only get unique values. If you are interested, please attach sample XML that is being applied to this Format and I'd be happy to help. To obtain the sample XML, edit the Format and select the appropriate Block and context Page within the Preview Options section at the top.

    Please let me know if you have any questions.

    Thanks!

  2. 2 Posted by lori on 25 May, 2015 07:21 PM

    lori's Avatar

    Hi Ryan,
    I haven't quite got this figured out. Can you have a peak at my code and sample XML?

    #set( $pageTitle = $_XPathTool.selectSingleNode($contentRoot, "//system-page[@current]/title") )
    #set ( $sectionTitle = $_XPathTool.selectSingleNode($contentRoot, "//system-page[@current]/system-data-structure/sectionTitle") )
    <h1>$_EscapeTool.xml($pageTitle.value)</h1>
        #if($sectionTitle.value != "")
            <h2 class="subtitle">$_EscapeTool.xml($sectionTitle.value)</h2>
        #end
        <hr />
    #set( $sections = $_XPathTool.selectNodes($contentRoot, "//system-page[name != 'index' ") )
         $_SortTool.addSortCriterion("dynamic-metadata[value = 'chair']", "", "text", "descending", "")
         $_SortTool.addSortCriterion("dynamic-metadata[value = 'assistant']", "", "text", "descending", "")
         $_SortTool.addSortCriterion("dynamic-metadata[value = 'faculty']", "", "text", "descending", "")
         $_SortTool.sort($sections)
    
        #foreach ( $section in $sections )
            #set($pages = $_XPathTool.selectNodes($contentRoot, "//system-page[name != 'index' and dynamic-metadata[name = 'position' and value = '${section}']]"))
            <h2>${section}</h2>
            #if ( $pages.size() > 0 )
                #foreach ( $page in $pages )
                    #set( $name = $page.getChild('title').value )
                    #set( $data = $page.getChild('system-data-structure') )
                    #set( $title = $data.getChild('title').value )                
                    #set( $image = $data.getChild('image').getChild('path').value )
                    #set( $contactInfo = $data.getChild('contactInfo') )
                    #set( $phone = $contactInfo.getChild('phone').value )
                    #set( $office = $contactInfo.getChild('office').value )
                    #set( $building = $contactInfo.getChild('building').value )
                    #set( $email = $contactInfo.getChild('email').value )
                    #set( $topics = $_XPathTool.selectNodes($data, "interests/topic") )
                    #set( $areas = $data.getChild('areas').value )
                    #set( $courses = $data.getChild('courses').value )
                    #set( $publications = $data.getChild('publications').value )
                    #set( $website = $page.getChild('link').value )
                    <div style="display:block;clear:both;">
                    <p>
                        <img alt="${name}" src="${image}" width="150px" class="" />
                        <strong>$name</strong>#if ($title != "")<strong>, $title </strong>#end
                        
                        <br />
                        <strong>Contact:</strong><br />
                        #if ($phone != "")
                            Phone: $phone <br />
                        #end
                        #if ($office != "")
                            Office: $office <br />
                        #end
                        #if ($building != "")
                            Building: $building <br />
                        #end
                        #if ($email != "")
                            Email: <a href="mailto:${email}">$_EscapeTool.xml($email)</a> <br /> 
                        #end
                        #if ($website != "")
                            Profile: <a href="${website}">$_EscapeTool.xml($name)</a><br />
                        #end
                        <br />
                    </p>
                    </div>
    
                #end
                <hr />
            #end
            <br />
        #end
    

    What I'm wanting to do is display all the pages that have the dynamic-metadata value "chair", then "assistant", then "faculty". Does that make sense?
    Thanks for your help!

  3. 3 Posted by Ryan Griffith on 27 May, 2015 01:41 PM

    Ryan Griffith's Avatar

    Hi Lori,

    Just to confirm, you would like to list these pages by position in that specific order? If that is the case, I don't think sorting would work since we're not really looking at an alphabetical order. Instead, I think you would be better off manually outputting the faculty and staff for each position type, so which would give you the most flexibility on order.

    Here is a sample Format I was working on that should accomplish this:

    #set( $pageTitle = $_XPathTool.selectSingleNode($contentRoot, "//system-page[@current]/title") )
    #set ( $sectionTitle = $_XPathTool.selectSingleNode($contentRoot, "//system-page[@current]/system-data-structure/sectionTitle") )
    <h1>$_EscapeTool.xml($pageTitle.value)</h1>
        #if($sectionTitle.value != "")
            <h2 class="subtitle">$_EscapeTool.xml($sectionTitle.value)</h2>
        #end
        <hr />
    
    ## Get the unique positions and add generate a lookup table.
    #set ( $lookup = {} )
    #set( $positions = $_XPathTool.selectNodes($contentRoot, "//system-page[name != 'index' and not(dynamic-metadata[name = 'position']/value = preceding::system-page/dynamic-metadata[name = 'position']/value)]/dynamic-metadata[name = 'position']/value") )    
    #foreach ( $position in $positions )
        #set($facstaff = $_XPathTool.selectNodes($contentRoot, "//system-page[name != 'index' and dynamic-metadata[name = 'position' and value = '${position.value}']]"))
        $!lookup.put($position.value, {
            "positionName": $position.value,
            "facstaff": $facstaff
        })
    #end
    
    ## Output faculty and staff for each position.
    #outputPosition($lookup.get("Chair"))
    #outputPosition($lookup.get("Assistant"))
    #outputPosition($lookup.get("Faculty"))
    
    #macro (outputPosition $position)
        #set ( $facstaff = $position.get("facstaff") )
        <h2>${position.get("positionName")}</h2>
        #if ( $facstaff.size() > 0 )
            #foreach ( $page in $facstaff )
                #set( $name = $page.getChild('title').value )
                #set( $data = $page.getChild('system-data-structure') )
                #set( $title = $data.getChild('title').value )                
                #set( $image = $data.getChild('image').getChild('path').value )
                #set( $contactInfo = $data.getChild('contactInfo') )
                #set( $phone = $contactInfo.getChild('phone').value )
                #set( $office = $contactInfo.getChild('office').value )
                #set( $building = $contactInfo.getChild('building').value )
                #set( $email = $contactInfo.getChild('email').value )
                #set( $topics = $_XPathTool.selectNodes($data, "interests/topic") )
                #set( $areas = $data.getChild('areas').value )
                #set( $courses = $data.getChild('courses').value )
                #set( $publications = $data.getChild('publications').value )
                #set( $website = $page.getChild('link').value )
                <div style="display:block;clear:both;">
                <p>
                    <img alt="${name}" src="${image}" width="150px" class="" />
                    <strong>$name</strong>#if ($title != "")<strong>, $title </strong>#end
                    
                    <br />
                    <strong>Contact:</strong><br />
                    #if ($phone != "")
                        Phone: $phone <br />
                    #end
                    #if ($office != "")
                        Office: $office <br />
                    #end
                    #if ($building != "")
                        Building: $building <br />
                    #end
                    #if ($email != "")
                        Email: <a href="mailto:${email}">$_EscapeTool.xml($email)</a> <br /> 
                    #end
                    #if ($website != "")
                        Profile: <a href="${website}">$_EscapeTool.xml($name)</a><br />
                    #end
                    <br />
                </p>
                </div>
    
            #end
            <hr />
        #end
        <br />
    #end
    

    Note: if you ever need to add/change the positions being displayed, simply modify the lines containing #outputPosition(...) to reflect the value of the position you wish to output.

    Please let me know if you have any questions.

    Thanks!

  4. 4 Posted by lori on 27 May, 2015 01:46 PM

    lori's Avatar

    I will try this! Thank you :)

  5. 5 Posted by Ryan Griffith on 27 May, 2015 01:58 PM

    Ryan Griffith's Avatar

    Not a problem at all, Lori. Please keep me posted on how the Format works out, or if you have any questions about the changes.

    Thanks!

  6. Support Staff 6 Posted by Tim on 17 Jun, 2015 02:22 PM

    Tim's Avatar

    Hi Lori,

    Any luck with Ryan's recommendations? Let us know how things are going.

    Thanks!

  7. 7 Posted by lori on 17 Jun, 2015 02:24 PM

    lori's Avatar

    Thanks for checking in. The recommendation made complete sense, but didn't quite render the results we're looking for. I'll post what I'm seeing a bit later today.

  8. 8 Posted by lori on 17 Jun, 2015 09:57 PM

    lori's Avatar

    So, with Ryan's recommendation, the output I'm getting is 3 loops (2 titled: ${position.get("positionName")} and one titled correctly "Faculty").. and all loops list all pages. This seems like the answer.. but something isn't working quite right.

  9. 9 Posted by Ryan Griffith on 18 Jun, 2015 12:25 PM

    Ryan Griffith's Avatar

    Thank you for the additional information, Lori.

    Based on what you are seeing, it sounds as though the code that builds out the lookup table isn't able to find the name of the position.

    When you have a moment, try using the following adjustment which will output the value of the position while building the lookup table so we can see what is being returned:

    #set( $positions = $_XPathTool.selectNodes($contentRoot, "//system-page[name != 'index' and not(dynamic-metadata[name = 'position']/value = preceding::system-page/dynamic-metadata[name = 'position']/value)]/dynamic-metadata[name = 'position']/value") )    
    #foreach ( $position in $positions )
       ## Debugging position name
       $position.value
    
        #set($facstaff = $_XPathTool.selectNodes($contentRoot, "//system-page[name != 'index' and dynamic-metadata[name = 'position' and value = '${position.value}']]"))
        $!lookup.put($position.value, {
            "positionName": $position.value,
            "facstaff": $facstaff
        })
    #end
    

    Please let me know if you have any questions.

    Thanks!

  10. 10 Posted by lori on 18 Jun, 2015 02:29 PM

    lori's Avatar

    Ok, I see what's going on now. It is working. The issue came when there were no pages associated with one of the position values we're looking up. I wrapped an 'if' statement around the outputs and it works perfectly! Thanks for all your help on this!

    The final code:

    #set( $pageTitle = $_XPathTool.selectSingleNode($contentRoot, "//system-page[@current]/title") )
    #set ( $sectionTitle = $_XPathTool.selectSingleNode($contentRoot, "//system-page[@current]/system-data-structure/sectionTitle") )
    
    <h1>$_EscapeTool.xml($pageTitle.value)</h1>
        #if($sectionTitle.value != "")
            <h2 class="subtitle">$_EscapeTool.xml($sectionTitle.value)</h2>
        #end
        <hr />
    ## Get the unique positions and add generate a lookup table.
    #set ( $lookup = {} )
    
    #set( $positions = $_XPathTool.selectNodes($contentRoot, "//system-page[name != 'index' and not(dynamic-metadata[name = 'position']/value = preceding::system-page/dynamic-metadata[name = 'position']/value)]/dynamic-metadata[name = 'position']/value") )
    #foreach ( $position in $positions )
       ## Debugging position name
       ## $position.value
        #set($facstaff = $_XPathTool.selectNodes($contentRoot, "//system-page[name != 'index' and dynamic-metadata[name = 'position' and value = '${position.value}']]"))
        $!lookup.put($position.value, {
            "positionName": $position.value,
            "facstaff": $facstaff
        })
    #end
    ## Output faculty and staff for each position.
    
    #if ($lookup.get("Chair") )
        #outputPosition($lookup.get("Chair"))
    #end
    #if ($lookup.get("Assistant") )
        #outputPosition($lookup.get("Assistant"))
    #end
    #if ($lookup.get("Faculty") )
            #outputPosition($lookup.get("Faculty"))
    #end
    #if ($lookup.get("Contract") )
        #outputPosition($lookup.get("Contract"))
    #end
    #if ($lookup.get("Sessional") )
        #outputPosition($lookup.get("Sessional"))
    #end
    #if ($lookup.get("Student Researcher") )
        #outputPosition($lookup.get("Student Researcher"))
    #end
    #if ($lookup.get("Senior Scholar") )
        #outputPosition($lookup.get("Senior Scholar"))
    #end
        
    #macro (outputPosition $position)
        #set ( $facstaff = $position.get("facstaff") )
        <h2>${position.get("positionName")}</h2>
        #if ( $facstaff.size() > 0 )
            #foreach ( $page in $facstaff )
                #set( $name = $page.getChild('title').value )
                #set( $data = $page.getChild('system-data-structure') )
                #set( $title = $data.getChild('title').value )
                #set( $image = $data.getChild('image').getChild('path').value )
                #set( $contactInfo = $data.getChild('contactInfo') )
                #set( $phone = $contactInfo.getChild('phone').value )
                #set( $office = $contactInfo.getChild('office').value )
                #set( $building = $contactInfo.getChild('building').value )
                #set( $email = $contactInfo.getChild('email').value )
                #set( $topics = $_XPathTool.selectNodes($data, "interests/topic") )
                #set( $areas = $data.getChild('areas').value )
                #set( $courses = $data.getChild('courses').value )
                #set( $publications = $data.getChild('publications').value )
                #set( $website = $page.getChild('link').value )
                <div style="display:block;clear:both;">
                <p>
                    <img alt="${name}" src="${image}" width="150px" class="" />
                    <strong>$name</strong>#if ($title != "")<strong>, $title </strong>#end
                    <br />
                    <strong>Contact:</strong><br />
                    #if ($phone != "")
                        Phone: $phone <br />
                    #end
                    #if ($office != "")
                        Office: $office <br />
                    #end
                    #if ($building != "")
                        Building: $building <br />
                    #end
                    #if ($email != "")
                        Email: <a href="mailto:${email}">$_EscapeTool.xml($email)</a> <br />
                    #end
                    #if ($website != "")
                        Profile: <a href="${website}">$_EscapeTool.xml($name)</a><br />
                    #end
                    <br />
                </p>
                </div>
            #end
            <hr />
        #end
        <br />
    #end
    
  11. 11 Posted by Ryan Griffith on 18 Jun, 2015 02:37 PM

    Ryan Griffith's Avatar

    Thank you for following up, Lori, I am glad to hear you were able to work through the issue and get things working.

    I'm going to go ahead and close this discussion, please feel free to comment or reply to re-open if you have any additional questions.

    Have a great day!

  12. Ryan Griffith closed this discussion on 18 Jun, 2015 02:37 PM.

Discussions are closed to public comments.
If you need help with Cascade CMS please start a new discussion.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac

 

26 Aug, 2016 01:19 PM
25 Aug, 2016 03:02 PM
25 Aug, 2016 12:50 PM
24 Aug, 2016 08:43 PM
24 Aug, 2016 07:20 PM
21 Aug, 2016 01:20 PM