How can I order the solutions of Solve from the one with the smallest real part to the one with the greatest real part?

  • 1
    $\begingroup$ SortBy[Re@*Last]@solutions? $\endgroup$ – kglr Feb 28 at 14:07
  • $\begingroup$ @kglr exactly thanks $\endgroup$ – mattiav27 Feb 28 at 14:12
  • $\begingroup$ it seems that solutions returned by Solve are already sorted in the desired order. $\endgroup$ – kglr Feb 28 at 14:23

You can wrap the output of Solve with SortBy[Re @* Last].

However, I think Solve already returns solutions in the desired order (sorted by the real parts with ties broken by the absolute values of their imaginary part).

Checking the conjecture using 1000 random polynomials:

polynomials = Table[TypeSystem`RandomPolynomial[], 1000];
solutions = DeleteCases[Solve[#[x] == 0., x] & /@ polynomials, {{}}];

solutions == SortBy[Re@*Last] /@ solutions


| improve this answer | |
  • 2
    $\begingroup$ I am considering a much more complicated expression containing continued fraction and the solution are not orderd in the correct way. If you want I can edit my post to add my expression $\endgroup$ – mattiav27 Feb 28 at 14:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.