Weirdness using sorter.sort for custom object list. | Community
Skip to main content
Digital_Pi22
Level 2
September 27, 2018
Question

Weirdness using sorter.sort for custom object list.

  • September 27, 2018
  • 1 reply
  • 4783 views

I have a velocity script as follows:

#set ($rawList = $CstPolicyList)

#set ($sortedList = $sorter.sort($rawList,"policySaleDate:desc"))

Here is Sorted List:

#if($sortedList.size() > 0)

  <table>

  <tr><th>ID</th><th>Sale Date</th></th></tr>

  #foreach($policy in $sortedList)

  <tr>

    <td> $policy.policyId</td>

    <td>$!policy.policySaleDate </td>

    </tr>

  #end

  </table>

#else

  Sorted list is empty! <br>

#end

---------

Here is Raw List:

#if($rawList.size() > 0)

  <table>

  <tr><th>ID</th><th>Sale Date</th></th></tr>

  #foreach($policy in $rawList)

  <tr>

    <td> $policy.policyId</td>

    <td>$!policy.policySaleDate </td>

   </tr>

  #end

  </table>

#end

policySaleDate is a Marketo Date field.  It may be empty.

When I run it, I get output for the second, "rawList".  But nothing for the sorted list (size is 0).

Even weirder, if I change the sort column to use updatedAt:

$sorter.sort($rawList,"updatedAt:desc"))

Or to

$sorter.sort($rawList,"updatedAt:asc"))

$sorter.sort($rawList,"updatedAt:desc"))

Then I get Identical output for both the sortedList and the rawList.  The "asc" and "desc" do not make a difference.

Why would the sort not return anything if I reference the "policySaleDate" field?  I know it is the correct name (and I have the check box checked in the list)

because the data shows up in the email for the $rawList

Message was edited by: Digital Pi fixed some typos

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.

1 reply

Digital_Pi22
Level 2
September 27, 2018

I experimented sorting by the policyId (text field):

#set ($sortedList = $sorter.sort($rawList,"policyId:desc"))

This works fine, it returns a sortedList to $sortedList as expected.

The issue seems to be with the custom object policySalesDate  date field.  $sorter.sort does not like it for some reason.

SanfordWhiteman
Level 10
September 27, 2018

It's not really weird! You can't mix types with a generic SortTool.sort, and comparing a null with a String value, or a Boolean with a String, or a Number with a Boolean, are all mixed types.  There isn't a comparator function to derive a useful result from such comparisons, so the sort will error out and you will get no result.

In Java at large you can build custom comparators for different types, so you ensure there's a result.  But in a locked-down Velocity scenario you can't do this. What you can do is iterate over the list and fill in any null with an empty-like value of the same type. Unfortunately null is the best non-value, and you can't use that or you're back where you started.