How to access and use TrackVia's API or Application Scripts?

Sorting records in Application Scripts?

It seems that sorting a List of records by a specific field requires the field value to be cast to the underlying data type? If you don't do that, it seems like the fields will sort by object identity or some other undesired key. Am I right about this?

For example, if I have a date field in a record, must I use
(Date)record["MyDateField"] to actually get a correctly sorting date.

1 vote
Vote
Sign in
(thinking…)
Sign in with: facebook google
Signed in as (Sign out)
You have left! (?) (thinking…)
Steve Goldman shared this idea  ·   ·  Flag idea as inappropriate…  ·  Admin →

1 comment

Sign in
(thinking…)
Sign in with: facebook google
Signed in as (Sign out)
Submitting...
  • Andrew Reid commented  ·   ·  Flag as inappropriate

    Hi Steve,

    When you get a list of records via an App Script, they'll be typically be sorted by the default sort. which is record created date. If you wish to sort by some other field in the record then it’s probably best to use the Groovy Spaceship Operator (http://groovy-lang.org/operators.html#_spaceship_operator).

    In the example below, I’m generating a list of child records from their common parent and then sorting them by a Date field in the child records. The second list (called “sortedKids”) returns all those children with the earliest date first.

    List<Map> kids = getChildren("Child Table", "Parent") as List<Map>
    List<Map> sortedKids = kids.sort({ a,b -> a['Date'] as Date <=> b['Date'] as Date }) as List<Map>

    As shown above, this does require casting objects as Dates in order for the sort to compile and execute properly.

Feedback and Knowledge Base