Search code examples
kdb

Sort zeros to end of list (stable)


how to sort zeros to end of the list. I tried with idesc function which is sorting all elements, but I want stable sort of the list and expected result is 2 4 3 2 0 0. Please help on this problem.

/this is not giving expected result, it is sorting all elements.
q)list:2 4 3 0 2 0
q)list (idesc list)
4 3 2 2 0 0     

 /Another option, Is there any better solution to optimize below function.
q){(x where x<>0), x where x=0}[list]
2 4 3 2 0 0

Solution

  • Your attempt was close, in this case it's better to idesc/iasc booleans to reorder

    q){x idesc x<>0}list
    2 4 3 2 0 0
    /or
    q){x iasc x=0}list
    2 4 3 2 0 0