sorting by custom data def field

Nando's Avatar


11 May, 2016 01:42 AM

all my pages come through fine but I can't figure out how to sort by my custom date field :(

#set ($speakersdata = $data.getChild("content-block").getChild("content").getChild("system-index-block"))##
#set ($speakerpages = $speakersdata.getChildren("system-page"))
$_SortTool.addSortCriterion("system-data-structure/speaker/date", "", "number", "ascending", "")  
#foreach ($speakerpage in $speakerpages)
do stuff

where did I go wrong? lol

thanks in advance!

  1. 1 Posted by Nando on 11 May, 2016 12:46 PM

    Nando's Avatar

    some more info:

    the date is using a date picker in the data def - does it have to be converted to a number first? but that would only be possible inside the for foreach.. .

  2. 2 Posted by Nando on 11 May, 2016 07:53 PM

    Nando's Avatar

    So I changed my date picker to use the date and time thinking it would allow me to sort by number... but that didn't help :(

  3. 3 Posted by Nando on 11 May, 2016 08:40 PM

    Nando's Avatar

    Got it!

    #set ($speakerpages = $_XPathTool.selectNodes($speakersdata, "system-page"))
    $_SortTool.addSortCriterion("system-data-structure/speaker/date", "", "text", "descending", "")

    2 things I changed:
    - use selectNodes instead of getChildren - use text instead of number

    I thought getChildren and selectNodes were interchangeable?

    The date comes in as a number but its not? its a string?

  4. 4 Posted by Wing Ming Chan on 12 May, 2016 01:55 PM

    Wing Ming Chan's Avatar


    getChildren and selectNodes are not interchangeable. getChildren is a method of org.jdom.Element. There are three versions of getChildren. When it accepts a single string argument, the string must be the name of an element. selectNodes is a static method of org.jdom.xpath.JaxenXPath, which is a subclass of org.jdom.xpath.XPath. When the method accepts an object and a string, the string should be the XPath expression, which of course can be the name, of an element. For more details, see my Velocity Tutorial, Lessons 1 and 2.

    Dates are strings. In fact, everything returned from getValue (or the corresponding .value property) are strings (see the org.jdom.Element API). To convert among various types, see Lesson 8: $_DateTool and $_. You may want to convert the strings to long numbers before sorting. This is more accurate.


  5. 5 Posted by Nando on 12 May, 2016 01:59 PM

    Nando's Avatar

    Thank you for the clarification Wing!

    How would I convert the date string to a number before the foreach?

    The only way I can come up with would be to create an array with with all data from the foreach and then do a new a foreach for the data in the array...

  6. 6 Posted by Wing Ming Chan on 12 May, 2016 02:12 PM

    Wing Ming Chan's Avatar

    See DateTool & SortTool for suggestions.


  7. Nando closed this discussion on 16 May, 2016 12:40 PM.

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

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


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