# Sort a Table

**URL:** <https://forum.kx.com/t/sort-a-table/13089>\
**Category:** Community Support\
**Tags:** kdb-and-q\
**Created:** [November 15, 2023, 4:41pm UTC](https://forum.kx.com/t/sort-a-table/13089 "2023-11-15T16:41:00Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![WilliamO11](https://avatars.discourse-cdn.com/v4/letter/w/4bbf92/32.png) [@WilliamO11](https://forum.kx.com/u/WilliamO11)\
**Post date:** [November 15, 2023, 4:41pm UTC](https://forum.kx.com/t/sort-a-table/13089/1 "2023-11-15T16:41:00Z")

</div>

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

&nbsp;

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

&nbsp;

&nbsp;

---

<div class="post-metadata">

**Author:** ![rocuinneagain1](https://avatars.discourse-cdn.com/v4/letter/r/fbc32d/32.png) [@rocuinneagain1](https://forum.kx.com/u/rocuinneagain1)\
**Post date:** [November 15, 2023, 4:55pm UTC](https://forum.kx.com/t/sort-a-table/13089/2 "2023-11-15T16:55:00Z")

</div>

For normal sorting operations see:

- https://code.kx.com/q/ref/asc/
- https://code.kx.com/q/ref/desc/&nbsp;

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

- https://code.kx.com/q/ref/rotate/&nbsp;

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

&nbsp;

&nbsp;

---

<div class="post-metadata">

**Author:** ![Rolf1](https://avatars.discourse-cdn.com/v4/letter/r/7c8e57/32.png) [@Rolf1](https://forum.kx.com/u/Rolf1)\
**Post date:** [November 15, 2023, 5:50pm UTC](https://forum.kx.com/t/sort-a-table/13089/3 "2023-11-15T17:50:00Z")

</div>

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

```

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