# Disabling Mathematica's automatic sort of output

I used Mathematica to greatly simplify an algebraic expression. I now want to copy it into some notes, but it by defaults things in a very non-intuitive manner. I have a sorting function that will sort the expression properly, but the output still reverts to Mathematica's default sorting algorithm.

As an example, let's say I want it to output: f[b] f[a]. The sorting function

In:= sorted = Sort[Level[f[b] f[a], 1], OrderedQ[{(#2)[], (#1)[]}] &]

Out= {f[b], f[a]}


properly returns a sorted list. But when I then apply the Times function to bring it back to an algebraic expression, it goes back to the default way of sorting:

In:= Apply[Times, sorted]

Out= f[a] f[b]


Is there a trick for formatting the output of an expression to match the ordering I would like? Preferably something I can can both view directly in Mathematica as well as copy to LaTeX.

Times has the Orderless attribute so this sorting cannot be prevented if you also want to do calculations with the expression. However, if you only need to display it once, then you can do something like this:

times[f[b], f[a]]


sorted as you like. The just wrap it in HoldForm and replace times with Times.

HoldForm[
times[f[b], f[a]]
] /. times -> Times Instead of times you can of course just use List, but once you need to deal with more than a single type of operation (e.g. both multiplication and addition), it is convenient to use distinguishable heads.