Why not:
setkey(DT, x, y, z)
setkey(DT, x, y)
Use setkey
to sort by all three columns, and then remove the last column from the key by resetting it. Also, to address your concern about the sort being maintained, it is documented (2nd paragraph of Details section of data.table
documentation):
The sort is stable; i.e., the order of ties (if any) is preserved.
This means that when you sort by x
and y
, after you have sorted by x
, y
, and z
, the orders of z
within any set of x
-y
values will be undisturbed because they all tie wrt to the x
-y
values