Sort: Difference between revisions

From The K Language Wiki
Content added Content deleted
(z->x; add k7; rm unnecessary parens)
m (Lowercase title)
Line 1: Line 1:
{{lowercase title}}
{{primitive|^x|in=K7,K9}}
{{primitive|^x|in=K7,K9}}



Revision as of 21:23, 8 July 2022


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