sorting by custom data def field
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", "")
$_SortTool.sort($speakerpages)
#foreach ($speakerpage in $speakerpages)
do stuff
#end
where did I go wrong? lol
thanks in advance!
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
1 Posted by Nando on 11 May, 2016 12:46 PM
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 Posted by Nando on 11 May, 2016 07:53 PM
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 Posted by Nando on 11 May, 2016 08:40 PM
Got it!
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 Posted by Wing Ming Chan on 12 May, 2016 01:55 PM
Hi,
getChildren
andselectNodes
are not interchangeable.getChildren
is a method of org.jdom.Element. There are three versions ofgetChildren
. When it accepts a single string argument, the string must be the name of an element.selectNodes
is a static method oforg.jdom.xpath.JaxenXPath
, which is a subclass oforg.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.Wing
5 Posted by Nando on 12 May, 2016 01:59 PM
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 Posted by Wing Ming Chan on 12 May, 2016 02:12 PM
See DateTool & SortTool for suggestions.
Wing
Nando closed this discussion on 16 May, 2016 12:40 PM.