sort

From The K Language Wiki
Sort
^x
Supported in K7,K9

The sort primitive sorts a list, dictionary, or table into ascending order.[1]

 ^0 3 2 1
0 1 2 3

Dictionaries are sorted using the keys and tables by the first column field.

^`b`a!(0 1 2;7 6 5)     / sort dictionary by key
[a:7 6 5;b:0 1 2]

One can sort tables by arbitrary columns by first reordering the columns in the table using take or by extracting the sort column by index or expression.

 ^[[]z:`c`a`b;y:3 2 1]      / sort table by 1st col
z y
- -
a 2
b 1
c 3

 ^`y`z#[[]z:`c`a`b;y:3 2 1] / sort table by new 1st col
y z
- -
1 b
2 a
3 c

References[edit]