4
$\begingroup$

I want to sort some exponential numbers like {2^2,3^3,4^2} and i want The result {2^2,4^2,3^3}. But Mathematica Sort function apply's the powers in the result and return's {4,16,27}. How can i get the result in the same form as input?

$\endgroup$
3
  • 8
    $\begingroup$ Something like SortBy[Inactivate[{2^2, 3^3, 4^2}, Power], Activate]? $\endgroup$ Commented May 12, 2020 at 13:43
  • $\begingroup$ @J. M.: Exactly,Thank you. $\endgroup$
    – Arman
    Commented May 12, 2020 at 14:06
  • $\begingroup$ If you understand how that snippet works, please consider answering your own question. $\endgroup$ Commented May 12, 2020 at 17:07

1 Answer 1

5
$\begingroup$

All credits goes to J. M.'s technical difficulties♦

The trick is to use the Inactivate function to prevent Mathematica to apply the powers.

Inactivate[{2^2, 3^3, 4^2}, Power]

Then use SortBy function to compare the Active forms of the elements.

SortBy[Inactivate[{2^2, 3^3, 4^2}, Power], Activate]

Result:

{2^2,4^2,3^3}
$\endgroup$
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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