tag:help-archives.hannonhill.com,2010-02-09:/discussions/velocity-formats/9426-sortingCascade CMS: Discussion 2014-03-26T15:31:10Ztag:help-archives.hannonhill.com,2010-02-09:Comment/321388552014-03-17T23:23:45Z2014-03-17T23:23:46ZSorting<div><p>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?</p>
<p>"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.</p>
<p>substring("MM-DD-YYYY", 1, 2) will give you the month<br>
substring("MM-DD-YYYY", 4, 2) will give you the day<br>
substring("MM-DD-YYYY", 7, 4) will give you the year</p>
<p>The Date picker should always give you a two digit month and
day."</p></div>Ryan Singzontag:help-archives.hannonhill.com,2010-02-09:Comment/321388552014-03-18T14:35:06Z2014-03-18T14:35:06ZSorting<div><p>Hi Ryan,</p>
<p>I think that original discussion may have the two fields
mixed.</p>
<p>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):</p>
<pre>
<code>$_SortTool.addSortCriterion("path/to/date-field", "", "number", "ascending", "upper-first")
$_SortTool.sort($pages)</code>
</pre>
<p>Note: <strong>/path/to/date-field</strong> should be a valid
path to your date field in the context of your
<strong>$pages</strong> variable. For example, if you are grabbing
<strong>system-page</strong> and the field is at the top level of
your data definition, your path would most likely be
<code>system-data-structure/date-field</code>. Also, you may need
to adjust the <strong>ascending</strong> option depending on the
direction you need to sort in.</p>
<p>The value of a datepicker alone is a string in the form of
<code>MM-DD-YYYY</code>, 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:</p>
<pre>
<code>## 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")
$_SortTool.sort($pages)</code>
</pre>
<p>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.</p>
<p>Please let me know if you have any questions.</p>
<p>Thanks!</p></div>Ryan Griffithtag:help-archives.hannonhill.com,2010-02-09:Comment/321388552014-03-18T15:38:24Z2014-03-18T17:49:18ZSorting<div><p>Thanks, I’m trying for the datepicker alone with the form
MM-DD-YYYY</p>
<p>Here’s a code portion of what I have:</p>
<pre>
<code>$_SortTool.addSortCriterion("substring(system-data-structure/news-date, 7, 4)", "", "text", "descending", "upper-first")
$_SortTool.sort($articles)
#foreach ( $article in $articles)
#set ($news_date = $_XPathTool.selectSingleNode( $article, 'system-data-structure/news-date').value )
……</code>
</pre>
<p>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?</p>
<p>Thank you,</p>
<p>Ryan</p></div>Ryan Singzontag:help-archives.hannonhill.com,2010-02-09:Comment/321388552014-03-18T17:51:48Z2014-03-18T17:51:48ZSorting<div><p>Hm, everything seems in order there. Perhaps the sorting is not
properly handling the number as a String.</p>
<p>When you have a moment, try the following and let me know if the
results differ:</p>
<pre>
<code>$_SortTool.addSortCriterion("number(substring(system-data-structure/news-date, 7, 4))", "", "number", "descending", "upper-first")</code>
</pre>
<p>This should convert the substring to a number and sort using a
numeric data type, rather than a string.</p>
<p>Please let me know if you have any questions.</p>
<p>Thanks!</p></div>Ryan Griffithtag:help-archives.hannonhill.com,2010-02-09:Comment/321388552014-03-18T18:23:08Z2014-03-18T18:23:09ZSorting<div><p>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.</p></div>Ryan Singzontag:help-archives.hannonhill.com,2010-02-09:Comment/321388552014-03-18T20:00:38Z2014-03-18T20:00:38ZSorting<div><p>Thank you for testing, Ryan.</p>
<p>For a minute there I thought I was going crazy. It looks as
though there is a <a href=
"https://hannonhill.jira.com/browse/CSI-743">known issue</a> 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.</p>
<p>Feel free to use this link to track the progress of this issue
or keep an eye out for it on our <a href=
"http://www.hannonhill.com/kb/Release-Notes">Release Notes
site</a>.</p>
<p>In the meantime, I think your best bet may be to switch over to
use XSLT for this one until the issue is resolved.</p>
<p>Here is a sample XSLT Format that should be a good starting
point:</p>
<pre>
<code><?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output encoding="UTF-8" indent="yes" method="html" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"/>
<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:apply-templates>
</xsl:template>
<xsl:template match="system-page">
<xsl:value-of select="system-data-structure/date"/>
</xsl:template>
</xsl:stylesheet></code>
</pre>
<p>Note: I was using a Content Type Index Block in this
example.</p>
<p>If you need to format the date differently, you can include and
use <a href=
"https://github.com/hannonhill/XSLT-Workbook/blob/master/Uncategorized/format-date.xsl">
this date formatter XSLT Format</a>. Usage would be as follows:</p>
<pre>
<code><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>
</xsl:call-template>
</xsl:template></code>
</pre>
<p>Please let me know if you have any questions.</p>
<p>Thanks!</p></div>Ryan Griffith