# how to sort an expression with descending coefficients?

I have an expression for example

psi = 0.24 FF1[0] FF2[1] FF3[-1] + 0.23 FF1[0] FF2[0] FF3[1] + 0.5 FF1[1] FF2[0] FF3[1] + 0.25 FF1[1] FF2[1] FF3[1]


I wonder how to get the expression with coefficients sorted in descending, such as

psi = 0.5 FF1[1] FF2[0] FF3[1] + 0.25 FF1[1] FF2[1] FF3[1]+ 0.24 FF1[0] FF2[1] FF3[-1] + 0.23 FF1[0] FF2[0] FF3[1]


Or get the list as

{{0.5,FF1[1] FF2[0] FF3[1]}, {0.25, FF1[1] FF2[1] FF3[1]}, {0.24,FF1[0] FF2[1] FF3[-1]}, {0.23, FF1[0] FF2[0] FF3[1]}}

What I only can do is just to extract the coefficients:

psi = 0.24 FF1[0] FF2[1] FF3[-1] + 0.23 FF1[0] FF2[0] FF3[1] + 0.5 FF1[1] FF2[0] FF3[1] + 0.25 FF1[1] FF2[1] FF3[1]
FCoefficients[HoldPattern[+z__]] := {z}
FCoefficients[else_] := {else}


Now I extract all the terms:

ListCoefficients = FCoefficients[targetstate]


The output is :

{0.24 FF1[0] FF2[1] FF3[-1], 0.23 FF1[0] FF2[0] FF3[1], 0.5 FF1[1] FF2[0] FF3[1], 0.25 FF1[1] FF2[1] FF3[1]}


What I want now is to sort the coefficients and its corresponding terms from ListCoefficients in descending. I can get the descending coefficients:

cofnum = ListCoefficients /. {FF1[l1_] FF2[l2_] FF3[l3_] -> 1};
Sort[cofnum, Greater]


The out put is:

{0.5, 0.25, 0.24, 0.23}


Is there any good way to do get psi = 0.5 FF1[1] FF2[0] FF3[1] + 0.25 FF1[1] FF2[1] FF3[1]+ 0.24 FF1[0] FF2[1] FF3[-1] + 0.23 FF1[0] FF2[0] FF3[1] ?

Or is it possible to get something as following:

{{0.5,FF1[1] FF2[0] FF3[1]}, {0.25, FF1[1] FF2[1] FF3[1]}, {0.24,FF1[0] FF2[1] FF3[-1]}, {0.23, FF1[0] FF2[0] FF3[1]}}

If anyone knows how to do that, would be very cool if you can give me some hints. Thank you very much!

• Why do you want to do this? Also, your last comment about using Total will not work... if you apply Total to the list it will sort the terms in a way you don't want. – bill s May 7 '19 at 14:22
• yes, you are right. The total doesn't not work. Sorry, I didn't check further. I will change the question. – Xuemei Gu May 7 '19 at 14:25
• @bills, because I want to pick up the three terms with large coefficients – Xuemei Gu May 7 '19 at 14:27
• Might be this: SortBy[List @@ psi, First] // Reverse? – Fred Simons May 7 '19 at 14:29
• @FredSimons, wow, thanks a lot. With SortBy[List @@ psi, First] // Reverse, I get the {0.5 FF1[1] FF2[0] FF3[1], 0.25 FF1[1] FF2[1] FF3[1], 0.24 FF1[0] FF2[1] FF3[-1], 0.23 FF1[0] FF2[0] FF3[1]}`, cool! – Xuemei Gu May 7 '19 at 14:33