# Sort a list of derivatives

I have a list of variables, both constant and functions of an independent variable, including derivatives. I'd like to sort these by the variable name, rather than the derivative order. For example, the default Sort gives:

Sort[{x[t], x'[t], x''[t], y[t], y'[t], y''[t],a}]
{a, x[t], y[t], x'[t], y'[t], x''[t], y''[t]}


whereas I'd like to have all the derivatives of one variable clustered together:

{a, x[t], x'[t], x''[t], y[t], y'[t], y''[t]}


I'm sure there is a way to write an ordering function to do this, but I can't figure out how to do it.

• A judicious use of SortBy[] ought to work… – J. M. will be back soon Aug 12 '15 at 9:12

As suggested in the comments, SortBy should work. For example you could do

f[Derivative[_][x_][_]] := x
f[x_[_]] := x
f[x_] := x

SortBy[{a, x[t], y[t], x'[t], y'[t], x''[t], y''[t]}, f]


One should check how robust this solution is though.

Edit

And a more compact way of writing f is

f[Derivative[_][x_][_] | x_[_] | x_] := x