Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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 :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading