Please Explain asc Operator Sort Order!

q)asc (0; 0 0; 0 0 0; (0 0; 0 0); ((0 0; 0 0);(0 0; 0 0)); enlist0; (enlist 0;enlist 0);enlist 0 0;enlist (0 0;0 0); (();());enlist ())

0 / (),rank 0

() / 0,rank 1

,() / 1 x0, rank 1 **

(();()) / 2 x0, rank 2 **

,(0 0;0 0) / 1 x2 x 2, rank 3

((0 0;0 0);(0 0;0 0)) / 2 x2 x 2, rank 3

(,0;,0) / 2 x1, rank 2

,0 0 / 1 x2, rank 2

(0 0;0 0) / 2 x2, rank 2

,0 / 1,rank 1

0 0 / 2,rank 1

0 0 0 / 3,rank 1


Based on the observed pattern, I would have expected the starred (**) items to have been sorted below the two rank 3 lines.


Can anyone give me any insight into how the asc operator handles these items?

I don’t exactly have any insight into the below, mainly because in 4 years of using q I have honestly never come across a situation where I would have to sort something like this. Is this an actual use case, or mainly just for curiosity? Either one of course is a valid enough reason to ask, but was wondering what kind of situation led to this. 

If in doubt, I would simply suggest creating a function that maps each item to a numeric value, which should then let you use iasc and get your sort in an order you desire without any ambiguity.

Hi José,

No not an actual use case. I am writing a book on Q Programming and wanted to document it’s behavior further than the simple list example in the Borror book.  I have a handle on it now.  Those two lists are untyped and so sort before the typed lists.

Thanks, Erik

Sent from my iPhone

Hi Erik,

Thanks for digging out all these interesting aspects of q. But I wonder if this sorting behaviour (i.e., by type of the elements) is going to be preserved in future kdb+ upgrades. Is this behaviour part of the internal sorting algorithm, or is it merely a coincidence?

Hi Erik,

Thanks for digging out all these interesting aspects of q. But I wonder if this sorting behaviour (i.e., by type of the elements) is going to be preserved in future kdb+ upgrades. Is this behaviour part of the internal sorting algorithm, or is it merely a coincidence?

This is documented (emphasis mine):

Function asc sorts a list.

  • on a simple list asc acts as expected, and sets the `s# attribute indicating that the list is sorted.
  • on a mixed list, it sorts within datatype.Enter code here…

(type of generic list =  0) < (7 = type of list of longs)

Thanks for the link Igor.  It doesn’t explain sorting of same typed item of different ranks or item with the same data type and rank, but different shapes.  But that’s OK I have figured out what it’s doing with a more complete list of expressions.  Appreciate everyone’s input.

I believe the sorting within and by rank is deliberate (and quite interesting) in the way it’s done.  There was definitely a lot thought put into it! 

From what I am observing (and I can be wrong of course) q follows J here: http://www.jsoftware.com/help/learning/16.htm (start reading from 16.2 Sorting, continue through 16.2.1 Predefined Collating Sequences that documents sorting boxed elements).

You’re probably right. J added a number of useful extensions to APL.  Of course J and APL both support higher order arrays and Q doesn’t, but does a pretty good job of simulating them.  Some feel there’s still a place for higher order arrays.

Sent from my iPhone