# Sort the terms of expression by the given order?

Let's assume I have the following expression :

D[K[x,y],{x,1}] + D[J[x,y],{x,2},{y,2}] + D[U[x,y],{x,2},{y,1}].


Can we sort it by the order corresponding to given list of dependent variables. For example in case of depVars = {J, U, K}, I need to get:

D[J[x,y],{x,2},{y,2}] + D[U[x,y],{x,2},{y,1}] + D[K[x,y],{x,1}].


Is it possible to write such sorting function ?

• Plus[] is Orderless, so any sorting of terms would be mere pretty-printing. – J. M.'s technical difficulties Nov 1 '17 at 14:42
• I think it can be transferred into list by /.Plus->List in order to transfer back after sorting. – Davit Shahnazaryan Nov 1 '17 at 14:48
• Sure, I'm just saying that no matter what you do, b + a will be converted to a + b. – J. M.'s technical difficulties Nov 1 '17 at 14:51
• Hmm, now got it. That's bad !!! – Davit Shahnazaryan Nov 1 '17 at 14:52
• I tried. For Mathematica the priority is the derivation orders. So it is converted into : D[K[x,y],{x,1}] + D[U[x,y],{x,2},{y,1}] + D[J[x,y],{x,2},{y,2}] – Davit Shahnazaryan Nov 1 '17 at 14:54