cancel
Showing results for 
Search instead for 
Did you mean: 

Sort a Table

WilliamO1
New Contributor II
New Contributor II

I want to sort to sort the table below with c and d at the top, anyone know the best way to achieve this? 

 

tab:([] sym:`a`b`c`d`e; x:5?10)

 

 

1 ACCEPTED SOLUTION

rocuinneagain
Valued Contributor
Valued Contributor

For normal sorting operations see:

q)tab:([] sym:`a`b`c`d`e; x:5?10)
q)tab
sym x
-----
a   8
b   1
c   9
d   5
e   4
q)`sym xdesc tab
sym x
-----
e   4
d   5
c   9
b   1
a   8

For your request perhaps rotate is what you are looking for

q)2 rotate tab
sym x
-----
c   9
d   5
e   4
a   8
b   1

 

 

View solution in original post

2 REPLIES 2

rocuinneagain
Valued Contributor
Valued Contributor

For normal sorting operations see:

q)tab:([] sym:`a`b`c`d`e; x:5?10)
q)tab
sym x
-----
a   8
b   1
c   9
d   5
e   4
q)`sym xdesc tab
sym x
-----
e   4
d   5
c   9
b   1
a   8

For your request perhaps rotate is what you are looking for

q)2 rotate tab
sym x
-----
c   9
d   5
e   4
a   8
b   1

 

 

Rolf
New Contributor III
q)t:([]sym:`a`b`c`d`e;x:til 5)
q)t iasc`c`d?t`sym
sym x
-----
c 2
d 3
a 0
b 1
e 4

 if you want to move the desired records to the top but want to preserve the order of the rest of the table