query custom sort - array add
I'm attempting to use a custom sort using the query tool, the
reason is we have many thousand of this content type and I was
hoping to only pull back the 50 most recently edited. I was using
the following page as a reference, however the .push didn't seem to
work, so I changed it to .add:
http://help-archives.hannonhill.com/discussions/velocity-formats/12...
When I changed it to .add, it now echo's "true" each loop that it
successfully adds the element to the array, everything else is
working great! Any ideas on how to remove this echo of "true" or
possibly a better way to write this code? I'm using 7.14.2,
Thanks!
#set($query = $_.query())
#set($query = $query.byContentType("site://www/News Article").maxResults(50).sortBy("modified").sortDirection("desc"))
#set($events = $query.execute())
#set ( $eventsToSort = [] )
#if ($events.size() > 0)
#foreach($event in $events)
#if (!$_PropertyTool.isNull($event.getStructuredDataNode("starts").textValue))
$event.getStructuredDataNode("starts").textValue
$!eventsToSort.add({
"startDate": $event.getStructuredDataNode("starts").textValue,
"pageObj": $event
})
#end
#end
#foreach ($event in $_SortTool.sort($eventsToSort, "startDate:asc"))
#set ( $page = $event.get("pageObj") )
#set ($starts = $_DateTool.getDate($page.getStructuredDataNode('starts').textValue))
$_DateTool.format('medium', $starts) $page.link $page.name $page.metadata.title
#end
#end
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 Wing Ming Chan on 17 Mar, 2016 12:56 PM
Hi Eric,
I don't know what the purpose of using the exclamation mark in
$!eventsToSort
is. But you need to know that$eventsToSort
is a java.util.ArrayList object. There is nopush
method defined in this class, but there are twoadd
methods defined:If you use the first one, it returns a boolean, hence the value
true
. You need to assign the returned value to a dummy variable like this:Or you can use the second method that does not return anything:
Wing
2 Posted by Eric on 18 Mar, 2016 03:36 PM
Thanks Wing! That indeed fixed my issue, I'm not sure what the exclamation mark does either, but saw it in a previous example that I copied and forgot to take it out.
Eric closed this discussion on 18 Mar, 2016 03:36 PM.
Wing Ming Chan re-opened this discussion on 18 Mar, 2016 03:57 PM
3 Posted by Wing Ming Chan on 18 Mar, 2016 03:57 PM
Eric,
Sorry that I was testing you wrt the exclamation mark. Of course there is a purpose of introducing it. It is documented in the Velocity User Guide. But you don't need it here in your code.
Wing
Tim closed this discussion on 21 Mar, 2016 01:51 PM.