Ryan Singzon's Avatar

Ryan Singzon

17 Mar, 2014 11:23 PM

I was reading another discussion about sorting with the Calendar Date picker. There was one suggestion about using XPath functions. Can you provide an example of the proper syntax to get it working?

"The second is using XPath functions to select the parts of the Date picker you'd like to sort with and creating multiple .addSortCriterion() for use by your .sort() method.

substring("MM-DD-YYYY", 1, 2) will give you the month
substring("MM-DD-YYYY", 4, 2) will give you the day
substring("MM-DD-YYYY", 7, 4) will give you the year

The Date picker should always give you a two digit month and day."

  1. 1 Posted by Ryan Griffith on 18 Mar, 2014 02:35 PM

    Ryan Griffith's Avatar

    Hi Ryan,

    I think that original discussion may have the two fields mixed.

    If you are using the date and time picker, the value will be a numeric timestamp that you can easily sort on using one criterion (since timestamps are just incrementing seconds):

    $_SortTool.addSortCriterion("path/to/date-field", "", "number", "ascending", "upper-first")  

    Note: /path/to/date-field should be a valid path to your date field in the context of your $pages variable. For example, if you are grabbing system-page and the field is at the top level of your data definition, your path would most likely be system-data-structure/date-field. Also, you may need to adjust the ascending option depending on the direction you need to sort in.

    The value of a datepicker alone is a string in the form of MM-DD-YYYY, so you will need to split the sorting up into multiple criterion. Let's assume we're dealing with the same example from above, but it is a date picker:

    ## Year
    $_SortTool.addSortCriterion("substring(path/to/date-field, 7, 4)", "", "text", "ascending", "upper-first")
    ## Month
    $_SortTool.addSortCriterion("substring(path/to/date-field, 1, 2)", "", "text", "ascending", "upper-first")
    ## Day
    $_SortTool.addSortCriterion("substring(path/to/date-field, 4, 2)", "", "text", "ascending", "upper-first")

    Note: like the previous example, you may need to adjust the path to the field as well as the sorting order depending on which direction you need to sort in. Make sure the direction is the same for each; otherwise, you could have a situation where the years and months are one direction and the days are the other.

    Please let me know if you have any questions.


  2. 2 Posted by Ryan Singzon on 18 Mar, 2014 03:38 PM

    Ryan Singzon's Avatar

    Thanks, I’m trying for the datepicker alone with the form MM-DD-YYYY

    Here’s a code portion of what I have:

    $_SortTool.addSortCriterion("substring(system-data-structure/news-date, 7, 4)", "", "text", "descending", "upper-first")
    #foreach ( $article in $articles)
        #set ($news_date = $_XPathTool.selectSingleNode( $article, 'system-data-structure/news-date').value )

    In trying to sort just using the year, I get articles in this order: 2013, 2014, 2014, and 2013. Can you check what I might be doing wrong or might be missing?

    Thank you,


  3. 3 Posted by Ryan Griffith on 18 Mar, 2014 05:51 PM

    Ryan Griffith's Avatar

    Hm, everything seems in order there. Perhaps the sorting is not properly handling the number as a String.

    When you have a moment, try the following and let me know if the results differ:

    $_SortTool.addSortCriterion("number(substring(system-data-structure/news-date, 7, 4))", "", "number", "descending", "upper-first")

    This should convert the substring to a number and sort using a numeric data type, rather than a string.

    Please let me know if you have any questions.


  4. 4 Posted by Ryan Singzon on 18 Mar, 2014 06:23 PM

    Ryan Singzon's Avatar

    I copied it exactly as you have it and it still doesn't work. Any other ideas on what the issue may be? Let me know if you need any additional info.

  5. 5 Posted by Ryan Griffith on 18 Mar, 2014 08:00 PM

    Ryan Griffith's Avatar

    Thank you for testing, Ryan.

    For a minute there I thought I was going crazy. It looks as though there is a known issue with the 7.x Series in which complex XPath queries (looking like it's specific to ones that generate numbers) are no longer working with the Sort Tool.

    Feel free to use this link to track the progress of this issue or keep an eye out for it on our Release Notes site.

    In the meantime, I think your best bet may be to switch over to use XSLT for this one until the issue is resolved.

    Here is a sample XSLT Format that should be a good starting point:

    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet xmlns:xsl="" version="1.0">
        <xsl:output encoding="UTF-8" indent="yes" method="html" xmlns:xsl=""/>
        <xsl:template match="/system-index-block">
            <xsl:apply-templates select="system-page">
                <xsl:sort select="substring(system-data-structure/date, 7, 4)"/>
                <xsl:sort select="substring(system-data-structure/date, 1, 2)"/>
                <xsl:sort select="substring(system-data-structure/date, 4, 2)"/>
        <xsl:template match="system-page">
            <xsl:value-of select="system-data-structure/date"/>

    Note: I was using a Content Type Index Block in this example.

    If you need to format the date differently, you can include and use this date formatter XSLT Format. Usage would be as follows:

    <xsl:template match="system-page">
            <xsl:call-template name="format-calendar-string">
                <xsl:with-param name="date" select="system-data-structure/date"/>
                <xsl:with-param name="mask">dddd, mmmm d, yyyy</xsl:with-param>

    Please let me know if you have any questions.


  6. Ryan Griffith closed this discussion on 26 Mar, 2014 03:31 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