query custom sort - array add

Eric's Avatar


17 Mar, 2016 12:10 AM

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,

#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
  1. 1 Posted by Wing Ming Chan on 17 Mar, 2016 12:56 PM

    Wing Ming Chan's Avatar

    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 no push method defined in this class, but there are two add methods defined:

    boolean add(E e)
    void    add(int index, E element)

    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:

    #set( $void = 
      $eventsToSort.add( {
        "startDate": $event.getStructuredDataNode("starts").textValue,
        "pageObj": $event
      } )

    Or you can use the second method that does not return anything:

        "startDate": $event.getStructuredDataNode("starts").textValue,
        "pageObj": $event


  2. 2 Posted by Eric on 18 Mar, 2016 03:36 PM

    Eric's Avatar

    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.

  3. Eric closed this discussion on 18 Mar, 2016 03:36 PM.

  4. Wing Ming Chan re-opened this discussion on 18 Mar, 2016 03:57 PM

  5. 3 Posted by Wing Ming Chan on 18 Mar, 2016 03:57 PM

    Wing Ming Chan's Avatar


    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.


  6. Tim closed this discussion on 21 Mar, 2016 01:51 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