KX Community

Find answers, ask questions, and connect with our KX Community around the world.
KX Community Guidelines

Home Forums kdb+ Sort a Table

  • Sort a Table

    Posted by williamo1 on November 15, 2023 at 12:00 am

    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)

     

     

    williamo1 replied 8 months, 2 weeks ago 3 Members · 2 Replies
  • 2 Replies
  • rocuinneagain

    Member
    November 15, 2023 at 12:00 am

    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

    Member
    November 15, 2023 at 12:00 am
    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

Log in to reply.