How do I sort dates with the sort tool?

Eric's Avatar


19 Aug, 2014 02:52 PM

First, here's my code. I'm trying to sort through the array $exhibitions in descending order by UNIX timestamp (date-picker tool). It's not working.

#set ( $exhibitions = $_XPathTool.selectNodes($contentRoot, "//system-page[dynamic-metadata[name='status' and value='Past']]") )
$_SortTool.addSortCriterion("start-date", "", "number", "descending", "")  
#if($exhibitions.size() == 0)
<p>There are no past exhibitions listed at this time.</p>
#foreach($exhibition in $exhibitions)
         #set($sd = $exhibition.getChild('system-data-structure'))
         #set($path = $exhibition.getChild('path').value)
         #set($name = $sd.getChild('name').text)
         #set($startdate = $_NumberTool.toNumber($sd.getChild('start-date').value))
         #set($enddate = $_NumberTool.toNumber($sd.getChild('end-date').value))
         #set($thumbnail = $sd.getChild('thumbnail').getChild('path').value)
         <div class="exhibit-list">
         <img src="$thumbnail" alt="$name"/>
         <span>$_DateTool.format('EEEEE MMMM d, yyyy', $startdate)</span>
         <br/><a href="$path">$name</a>

So, I'm trying to sort by 'start-date', which is a UNIX timestamp from the date-picker tool, and that should be sortable?

In the first line, I'm grabbing all the system-page elements. Then, I loop through each system-page element and grab 'system-data-structure'. 'start-date' is a child of 'system-data-structure', which, in turn, is a child of 'system-page' Is the selection string in .addSortCriterion() wrong? Or, am I taking the entirely wrong approach with this?

  1. 1 Posted by Ryan Griffith on 19 Aug, 2014 03:34 PM

    Ryan Griffith's Avatar

    Hi Eric,

    As you mentioned, it sounds as though you need to adjust the XPath used in your addSortCriterion call. When you have a moment, try the following and let me know how it works out:

    $_SortTool.addSortCriterion("system-data-structure/start-date", "", "number", "descending", "")
  2. 2 Posted by Eric on 19 Aug, 2014 04:45 PM

    Eric's Avatar

    Thank you, it worked beautifully.

  3. 3 Posted by Ryan Griffith on 19 Aug, 2014 06:03 PM

    Ryan Griffith's Avatar

    Thank you for following up, Eric, I am glad to hear you were able to sort the pages.

    Have a great day!

  4. Ryan Griffith closed this discussion on 19 Aug, 2014 06:03 PM.

Comments are currently closed for this discussion. You can start a new one.

Keyboard shortcuts


? 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