4
$\begingroup$

First output returned by Sort is correct - ordered from smaller to bigger numbers.

Second output returned by SortBy is correct but only in the sense if FullForm's are sorted, not values.

Third output returned by SortBy is correct - ordered from smaller to bigger number (because N was added in the code).

Fourth output return by SortBy is incorrect - we would need to add more decimal digits like with N[,100].

Sort[{-2, -(2^20 + 1)^(1/20)}, Less]
SortBy[{{-2, 2}, {-(2^20 + 1)^(1/20), 2}}, {#[[2]], #[[1]]} &]
SortBy[{{-2, 2}, {-(2^20 + 1)^(1/20), 2}}, N@{#[[2]], #[[1]]} &]
SortBy[{{-2, 2}, {-(2^50 + 1)^(1/50), 2}}, N@{#[[2]], #[[1]]} &]

(* {-1048577^(1/20),-2} *)
(* {{-2,2},{-1048577^(1/20),2}} *)
(* {{-1048577^(1/20),2},{-2,2}} *)
(* {{-2,2},{-5^(3/50) 9007199254741^(1/50),2}} *)

But now I want to use SortBy the same way like Sort - without converting to numerical values. How to do that?

$\endgroup$

1 Answer 1

9
$\begingroup$

You can use the three-argument-form of SortBy with NumericalOrder:

SortBy[{{-2, 2}, {-(2^20 + 1)^(1/20), 2}}, {#[[2]], #[[1]]} &, NumericalOrder]
(* {{-2,2},{-1048577^(1/20),2}} *)
$\endgroup$
1
  • $\begingroup$ If I recall correctly there was no third argument in older versions. $\endgroup$ Sep 16, 2023 at 18:51

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.