Skip to content

Sort

Name - Description Default Type
<input> Any input is ignored.
<output> Output is the sorted sequence. [Any]
From The name of the sequence variable to edit in place. None &[Any]
Join Other columns to join sort/filter using the input (they must be of the same length). None &[Any][&[Any]]
Desc If sorting should be in descending order, defaults ascending. false Bool
Key The shards to use to transform the collection's items before they are compared. Can be None. None Shard[Shard]

Sorts the elements of a sequence. Can also move around the elements of a joined sequence in alignment with the sorted sequence.

Details

Sort sorts all the elements of the sequence that has been passed to it in the :Name parameter based on the value of the :Desc parameter. If :Desc is set to true the sequence is sorted in descending order, else it's sorted in the ascending order (which is the default behaviour).

This shard can also take final element order of the sorted sequence and apply that to a joined sequence (passed via the :Join parameter). For example, if the element at index-7 moved to index-3 in the main sequence due to sorting then in the joined sequence too the element at index-7 would move to index-3. The movement of all elements in the main sequence (post-sort) would be mirrored in the joined sequence. For this to work both the sequences must have the same length.

Note

Think of this as the Shards equivalent of a relational database inner join. The main sequence and the joined sequence can be thought of as columns from two different tables inner joined over indices equality. So that the changes in elements of one sequence (rows in the first table) can be propogated to the corresponding elements of the joined sequence (corresponding rows in the joined table).

In this case the operation is changing of indices (position) of selected elements (selected rows) in one sequence (table) leading to an equivalent change of indices (position) of corresponding elements (connected rows) of the joined sequence (joined table).

The :Key parameter can take a shard or group of shards to transform the sequence elements before they're compared for sorting. This transformation doesn't actually change the value of the elements in the final sorted sequence (it's used only for sort comparisons).

Sort works only on sequences.

Any input to this shard is ignored and its output is the main sorted sequence.

See also

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
; sort ascending (since default :Desc is false)
[9 5 1 3 8] >= .seq1
(Sort :From .seq1)
.seq1 (Log "seq1")  ;; sorted ascending => seq1: [1, 3, 5, 8, 9]

; sorting descending (:Desc set to true)
[9 5 1 3 8] >= .seq2
(Sort .seq2 :Desc true)
.seq2 (Log "seq2")  ;; sorted ascending => seq2: [9, 8, 5, 3, 1]

;; sort ascending using a :Key
[9 5 1 3 8] >= .seq3
(Sort .seq3 :Key (Math.Multiply -1))
.seq3 (Log "seq3")  ;; :Key transform makes it look like descending sort => seq3: [9, 8, 5, 3, 1]

;; sort ascending with a joined sequence
[9 5 1 3 8] >= .seq4
["a" "b" "c" "d" "e"] >= .seqJ
(Sort .seq4 :Join .seqJ)
.seq4 (Log "seq4")  ;; main sequence sorted in ascending order => seq4: [1, 3, 5, 8, 9]
.seqJ (Log "seqJ")  ;; sorted order applied to joined sequence => seqJ: [c, d, b, e, a]
[info] [shards/General/Sort/Sort.edn] seq1: [1 3 5 8 9]
[info] [shards/General/Sort/Sort.edn] seq2: [9 8 5 3 1]
[info] [shards/General/Sort/Sort.edn] seq3: [9 8 5 3 1]
[info] [shards/General/Sort/Sort.edn] seq4: [1 3 5 8 9]
[info] [shards/General/Sort/Sort.edn] seqJ: [c d b e a]