Achieving this task is not at all trivial, since Mathematica has its internal order. It forces this order to all results in the OutputForm. As @march has already noticed the rearrangement often requires using such operations as those of the Hold group. The latter, however, prevents the expression from the possibility to make further transformations.
For this reason, if you are going to make some further Mathematica transformations with your result I recommend leaving it as it is, that is, in the order generated by Mathematica.
Nevertheless, the rearrangement of the result is an important task. To address the question asked by @march, the important reason to rearrange the results is the necessity to look at the result written in a convenient form. This often helps its better understanding and getting ideas of what to do with it during possible further calculations. For this reason, I often do such rearrangements after finalizing my calculations, or at least, after finalizing the current step of the calculations.
To do such rearrangements I have written a few functions two of which I share below with the explanations. After that, I address your examples to show how these functions work.
Rearrange terms in a sum
The function rearrange[expr,listOfFinalPositions]
rewrites a sum of several terms in the order prescribed by the list entitled "listOfFinalPositions."
Arguments: expr
is the expression with the head Plus
. listOfFinalPositions
is a list. Its length is equal to the length of the expression (Important). It indicates the positions that the terms of the sum must take in the end.
For example, for the sum a+b+c
the listOfFinalPositions
{2,3,1}
means that a
must go to the second position, b
- to the third, and c
- to the first one resulting in c+a+b
.
The function wraps the result by the HoldForm
function to forbid an undesired reordering. Therefore, to use the resulting expression further one needs to first apply ReleaseHold
to it.
rearrange[expr_, finalPositions_List] := Module[{lst, newlst},
lst = List @@ expr;
newlst =
Table[lst[[Position[finalPositions, i][[1, 1]]]], {i, 1, Length[lst]}];
HoldForm[Evaluate[expr]] /. MapThread[Rule, {lst, newlst}]
];
factorMinus
The function factorMinus
takes the sign "-" out of the expression expr
, and (if needed) applies a function fun
to -expr
. By default fun
is Identity
.
The function wraps the result by the HoldForm
function to forbid an undesired reordering. Therefore, to use the resulting expression further one needs to first apply ReleaseHold
to it.
factorMinus[expr_, fun_ : Identity] := (-1)*HoldForm[Evaluate[fun[(-1)*expr]]]
Examples of applications
Here I demonstrate the way to apply these two functions to the expressions you gave in the examples to your question
Example 1
Clear[expr1, expr2, expr3, expr4];
expr1 = (-a - b) (c - d)
(* (-a - b) (c - d) *)
expr2 = MapAt[factorMinus, expr1, {{1}, {2}}] // ReleaseHold
(* (a + b) (-c + d) *)
expr3 = MapAt[rearrange[#, {2, 1}] &, expr2, {2}]

Done. The last result here I show as an image. It is because it is wrapped by HoldForm
which uses Boxes when showing it here. The latter prevents from clearly seeing it. However, if one applies the ReleaseHold
to this final result Mathematica immediately rearranges it according to its internal order which is not desired. However, if you plan to further use this result in your calculations, do not forget to apply the ReleaseHold
first.
Example 2
Clear[expr1, expr2, expr3, expr4]
expr1 = ConditionalExpression[
1/3 g \[Rho] (2 Sqrt[-h^5 (h - 2 r)] -
r (Sqrt[-h^3 (h - 2 r)] + 3 Sqrt[-h (h - 2 r)] r) +
6 r^3 ArcCsc[Sqrt[2] Sqrt[r/h]]), h < 2 r]
Let us first get rid of the ConditionalExpression
expr2 = Simplify[expr1, h < 2 r]
(* 1/3 g \[Rho] (2 Sqrt[-h^5 (h - 2 r)] -
r (Sqrt[-h^3 (h - 2 r)] + 3 Sqrt[-h (h - 2 r)] r) +
6 r^3 ArcCsc[Sqrt[2] Sqrt[r/h]]) *)
expr3 = MapAt[factorMinus[#] &,
expr2, {{4, 1, 2, 1, 3}, {4, 2, 3, 1, 1, 3}, {4, 2, 3, 2, 2, 1,
3}}] // ReleaseHold
(* 1/3 g \[Rho] (2 Sqrt[h^5 (-h + 2 r)] -
r (3 r Sqrt[h (-h + 2 r)] + Sqrt[h^3 (-h + 2 r)]) +
6 r^3 ArcCsc[Sqrt[2] Sqrt[r/h]]) *)
expr4 = MapAt[rearrange[#, {2, 1}] &,
expr3, {{4, 1, 2, 1, 2}, {4, 2, 3, 1, 3, 1, 2}, {4, 2, 3, 2, 1, 2}}]

Done. This expression is again held.
Example 3
Clear[expr1];
expr1 = 1/2 (-a + b + Cos[a] Sin[a] - Cos[b] Sin[b])
1/2 (-a + b + Cos[a] Sin[a] - Cos[b] Sin[b])
MapAt[rearrange[#, {2, 1, 3, 4}] &, expr1, {2}]

Example 4
Clear[expr1];
expr1 = 1/(-1 + x^2)
(* 1/(-1 + x^2) *)
MapAt[rearrange[#, {2, 1}] &, expr1, {1}]

Example 5
Clear[expr1];
expr1 = Log[a - x]/(4 a^3) - (ArcTan[x/a]/(2 a^3)) - Log[a + x]/(4 a^3)
(* -(ArcTan[x/a]/(2 a^3)) + Log[a - x]/(4 a^3) - Log[a + x]/(4 a^3) *)
rearrange[expr1, {3, 1, 2}]

Have fun!
Simplify
to the resulting expression, but note that Mathematica outputs expressions in default forms. For instance, if you evaluateb + a
, it will output asa + b
, and there's no way around this unless you apply functions likeHoldForm
to the expression:HoldForm[a + b]
, but then you can't do anything with it afterwards until you applyReleasehold
. $\endgroup$FunctionPeriod[Sin[x], x]
, so rearranging terms of a trig expr is not useful. $\endgroup$