xasc and xdesc. Consider the following table:

q)table: ([] x: 10 ? "abc"; y: 10 ? til 10)
q)table
x y
---
a 7
a 7
b 1
b 9
a 1
a 0
c 8
b 8
c 3
c 1
q)

To sort by column x, instead of “ORDER BY x”, we write `x xasc table:

q)`x xasc table // sort by column x
x y
---
a 7
a 7
a 1
a 0
b 1
b 9
b 8
c 8
c 3
c 1
q)

The application of xdesc is similar.

In addition, to sort by multiple columns, instead of “ORDER BY y, x” or “ORDER BY y, x DESC, we pass the list of column names as the left argument to xasc or xdesc, respectively. For example,

q)`y`x xdesc select from table where y > 5
x y
---
b 9
c 8
b 8
a 7
a 7
q)

Don’t confuse xasc and xdesc with asc and desc, which operate on a vector instead of a table. Read more about sorting vectors in this related faq.

`s is one of q’s data attributes, and indicates that the data in the list is sorted. When q knows that a list is sorted, it can perform some operations much faster. In contrast to the other attribute types, the sorted attribute has zero memory overhead (no data structure needs to be created to support it):

q)d: (-10000 ? `4) ¡ til 10000
q)key d
`namk`eefb`dnkg`pfme`ndpk`bhck`bdga`hkdk`gpja`oiof..
q)value d
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 ..
q)\t do[100000; d key[d] 5000] 443
q)d: (asc key d) ! value[d] iasc key d
q)key d
`s#`aaab`aaak`aaap`aabb`aabn`aace`aadj`aaeg`aaeh`a..
q)\t do[100000; d key[d] 5000] 89
q)

Notice that value d above did not have the `s attribute; that is, the til function does not return a list with the `s attribute applied. In general, for a list to have the `s attribute, you must either sort the list (with asc as above) or let q know explicitly that the list is sorted by applying the `s attribute to a list that you know is already sorted:

q)x: `s # til 10
q)x
`s#0 1 2 3 4 5 6 7 8 9
q)

If you try to apply `s to a list that isn’t actually sorted, you’ll get an ‘s-fail error:

q)`s # 3 2 1
‘s-fail
q)

You can add elements to the end of a sorted list without losing the sorted attribute if the new elements do not violate the sorted-ness of the result:

q)x: `s # 1 2 3
q)x
`s#1 2 3
q)x ,: 4 5 6
q)x
`s#1 2 3 4 5 6
q)x ,: 7 7 7
q)x
`s#1 2 3 4 5 6 7 7 7

However, other modifications to a sorted list will cause the list to lose its attribute:

q)x: `s # 1 2 3
q)x
`s#1 2 3
q)x, 0
1 2 3 0
q)reverse x / reverse function removes it
3 2 1
q)

This is true even if the modifications result in a sorted list:

q) x: `s # 1 2 3
q)1 _ x
2 3
q)2 # x
1 2
q)

You can remove the sorted attribute explicitly by applying a null attribute to the list:

q)`#1 2 3 / application of a null attribute
1 2 3

See also the following function definitions: attr and #.

For even more detail, see Section 42 of the Abridged Q Language Manual.

Short answer: asc and desc:

q)asc 3 5 2 0 1
`s#0 1 2 3 5
q)desc 3 5 2 0 1
5 3 2 1 0
q)

Notice that, in the case of asc, q attaches the `s attribute to the result. (There is no corresponding attribute for a descending sort.)

As an aside, you may be surprised to discover that calling asc on an attribute-free list that happens to be in sorted order will have the `s attribute applied to it as a side effect:

q)list: til 1000
q)attr list
`
q)asc list
`s#0 1 2 3 4 5 6 7 8 9 10 ..
q)list
`s#0 1 2 3 4 5 6 7 8 9 10 ..
q)

Also take a look at iasc and idesc.

Finally, to sort a table instead of a list, see xasc and xdesc. Read more about sorting tables from this faq