_sort
Sort the received list --
Function
Anything -> _sort
(exp) -> The same Type
- Receives
- A List of any sort
- exp (optional)
- One or more expressions to apply to the received values, saying how to sort them
- Produces
- The same List, sorted as requested
With no parameters, _sort sorts the elements of the received List "naturally" -- alphabetically by their Names
if they are Things, in numeric order if they are Numbers, and so on.
This is what you want most of the time. However, note that many methods that return Lists are sorted to begin with,
so you often don't even need to bother.
If parameters are given, they are applied to each element in LIST, and the results are used to sort the
elements. The sort order is whatever is natural for the returned elements -- usually alphabetical, but might be, for example,
numeric if the results are numeric. It is essential that EXP return the same type for all elements, and it should return
ExactlyOne value. (If it returns a List, only the first will be used. The behaviour is undefined if it returns a Set, or None.)
If you need to sort on multiple fields, list them as additional parameters. For example, if you have
LIST -> _sort(A, B, C) -> SORTED
This will try to sort the list on A. When it finds multiple Things with the same value for A, it will sort them on B instead, then
C, and so on.
_sort is focused on Things, and as mentioned above, will default to sorting those by Name. You can also sort lists of Numbers,
and many Types just work correctly, but not all. If you need to sort something, and can't make it work, please speak up.
Most of the time, you will want exp to simply be the name of a Property. For example, this:
My Stuff._instance -> _sort(Title)
Produces all of the Instances of the "My Stuff" Model, sorted based on the "Title" Property. But it's possible to get much fancier if you need to:
exp can be any QL Expression that receives a Link and produces a consistent Type.
If you need to reverse the order of the sort, use the
_desc method inside of it.
If two or more elements being sorted have the same sort value, they will be sorted by Name.