Skip to main content
Level 2
February 1, 2019
Respondido

Custom object multi-level ordering/sorting

  • February 1, 2019
  • 7 comentários
  • 4888 Visualizações

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}

Este tópico foi fechado para respostas.
Melhor resposta por SanfordWhiteman

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

7 comentários

SanfordWhiteman
Level 10
February 1, 2019

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

Level 2
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
Level 10
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.