Sort: Difference between revisions

From The K Language Wiki
Content added Content deleted
("Better category ordering, add redirects to categories, fix pseudo-adverb categories")
("Trailing whitespace")
 
Line 1: Line 1:
{{primitive|^x|in=K7,K9}}
{{primitive|^x|in=K7,K9}}


The '''sort''' primitive sorts a list, dictionary, or table into ascending order.<ref>https://estradajke.github.io/k9-simples/k9/Verb.html#sort</ref>
The '''sort''' primitive sorts a list, dictionary, or table into ascending order.<ref>https://estradajke.github.io/k9-simples/k9/Verb.html#sort</ref>


<pre>
<pre>
Line 15: Line 15:


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.
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.

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

Latest revision as of 07:59, 9 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[edit]