Custom object multi-level ordering/sorting | Community
Skip to main content
New Participant
February 1, 2019
Solved

Custom object multi-level ordering/sorting

  • February 1, 2019
  • 1 reply
  • 4852 views

Hi everyone,

I'm trying to figure out how to sort custom objects on multiple levels and need some assistance. So I'd sort by date/time with most recent at the top and then sort by boolean field with "true" values at the top. The result would be the most recent record with "true" value at the top. The below is kind of what I'm trying to achieve but, of course, this gives me an error. Any help is appreciated!

#set( $codeByMostRecent = $sorter.sort($List_cList,"availabilityUpdated:desc") && $sorter.sort($List_cList,"Availability:desc") )

#set( $latest_code = $codeByMostRecent[0] )

## Output

${latest_code.code}

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

$sorter.sort($List_cList,["availabilityUpdated:desc","Availability:desc"])

1 reply

SanfordWhiteman
SanfordWhitemanAccepted solution
New Participant
February 1, 2019

$sorter.sort($List_cList,["availabilityUpdated:desc","Availability:desc"])

Jon_YangAuthor
New Participant
February 1, 2019

Hey Sanford,

Thank you for that. Didn't realize the solution was so simple...


Given how simple this script is, is there a circumstance in which this script would fail to output? I have a couple of records that I'm using to test this and 3 of the 4 records work but the last one just outputs token instead of the resulting code.

Also how would I account for records with the same date/time stamp with the value "true". Would a foreach be the simplest solution? Thanks in advance!

SanfordWhiteman
New Participant
February 1, 2019

Given how simple this script is, is there a circumstance in which this script would fail to output? I have a couple of records that I'm using to test this and 3 of the 4 records work but the last one just outputs token instead of the resulting code.

Yes, there is. If the sorter encounters different datatypes for the same property, including null values, when comparing, the sort will fail.

What's fascinating about this, and probably worth a short blog post, is that you will only have a problem when values must be compared to break ties. For any fixed set of values, the outcome is deterministic, of course. But you could have 2 lists that look similar-ish from a 1000ft view, and one would throw an error and the other wouldn't, simply because of how many comparisons were required and whether 2 incompatible datatypes needed to be compared.

Also how would I account for records with the same date/time stamp with the value "true". Would a foreach be the simplest solution? Thanks in advance!

Yes, if they're sorted #foreach and #break when you see the first false.