Rowan home > Reference > Sort functions

Sort Functions: and

Module: Additional2
Syntax: ⍋/⍒ array : int-vector (monad);
array1 ⍋/⍒ array2 : sorted-array1 (dyad);
customisable
Errors: none
Default Level: 0 (monad); 0 0 (dyad)
Keystrokes: AltGr+G, AltGr+Shift+G

When used monadically, returns the indices that would sort the array into ascending or descending order, so that array⌷⍋array will be the sorted array. This allows you to sort one array by the values in another.

When called dyadically, returns the left arguments sorted by the right. (That is, x⍋y is equivalent to (⍋y)⌷x.) The monadic composite function (grade reflex) returns a sorted version of its argument.

The comparison function used by default is numeric order for numeric arguments, and the CompareTo() method for any class which implements IComparable (this includes strings and other basic types). You can give a custom sort function by using the Customise operator :

   a←(3 5 2 1 5 4);
   b←("Three" "Five (1)" "Two" "One" "Five (2)" "Four");
   ⍋a
(4 3 1 6 2 5)
   ⍋⍨a
(1 2 3 4 5 5)
   b⍋a;
(One Two Three Four Five (1) Five (2) )
   b⍒a;
(Five (1) Five (2) Four Three Two One )
   b({(⍵%3)-(⍺%3)}⌾⍒)a;
(Five (1) Two Five (2) One Four Three )
The last grade in this example sorts the list by its modulo-3 value.

A custom sort function must be dyadic and return a boolean or numeric scalar. A value greater than or equal to 1, or a boolean true, indicates that the right argument () has a higher value; a zero that they have the same value; and a value less than or equal to -1, or a boolean false, that the left argument () has a higher value.