For detail on sorting a view using Date-types via Sort-group attributes, see Sorting Dates.
Sorting (lists) in action code requires knowing the differing implications of lexical vs. numerical sorting. If not familiar, read that article to better understand the rest of this article. The relevant issue is the two sort types sort the "never
" Date-type value differently:
- Lexical sort: "
never
" comes last, as strings starting with numbers sorts after strings starting with letters. Reversing lexical sort places said items first. - Numerical sort: "
never
" comes first, as non-numbers (strings) coerce to no number, i.e. zero (0
). Reversing numerical sort places said items last.
Does this list position matter?
No—except if we make assumptions that the "never
" value is always in the same position. As, it turns out, it may be sorted first or last, some care is needed when working with sorted date lists.
Sort operators
A small number of action code operators perform sorts explicitly. That is their primary function, sorting lists (List and Sets:
- .sort(). Lexical sort ("
never
" comes last). - .isort(). Case insensitive lexical sort ("
never
" comes last). - .nsort(). Numerical sort ("
never
" comes first).
Sets: Self-sorting lists
Be aware to that when placing Date-type data in a list, date is stored in a textual (string) for. The two attribute types behave differently:
- List type. The list items are stored in the order they were created/added to the list. Thus a "
never
"remains where it was entered. The presence of such values is best found by sorting the List, taking care to remember whether a lexical or numerical sort is being applied. - Set type. As sets automatically de-duplicate values, is involves an implicit lexical sort. This means if any Date-type input is a default, the Set will have a list item of "
never
".
List-creating operators
The collect(), collect_if(), and list() all return List-type content. The data is normally list of notes (as $Name/$Path/$ID/$IDString data) but can collect Date values directly if so coded by the user.
Operators with implied sort
The max(), min(), List.max(), and List.min(), implicitly sort data. They are intended for Number-type data, so if used—arguably incorrectly—on a list of Date values, this implies doing a numerical sort on a list of string values, albeit strings starting with numbers. As such any "never
" value present will sort at the beginning of the list.
Beware .first and .last
Do not mistake List.first() and List.last() as implying earliest/latest date. They simply return the first item in the list or the last, the same as using list[0] or List.at(-1).
Other sort-related tasks