3
$\begingroup$

If we try Sort[] with meaningless order-function like

Sort[{3, 2, 1}, True]

or,

Sort[{3, 2, 1}, False]

gives {3, 2, 1}, i.e., Sort[] does not do anything.

For the former case, I think it is natural because every comparison of two elements returns True, but what about the latter case?

I tried the visualization shown in the Wolfram document for Sort[], but I cannot understand why the sort converges.

$\endgroup$

1 Answer 1

5
$\begingroup$

First, True and False are not functions, they are just symbols. To demonstrate, evaluate

True[3, 2]

You should get back just True[3, 2] unevaluated. You can easily create a constant function, however, by just tacking on &:

True &[3, 2]

This time you should get back True.

So, in your original attempt, since you didn't get an actual boolean result for each comparison, no change was made. To force a constant comparison function, you could try these:

Sort[{3, 2, 1}, True &] (* {3, 2, 1} *)
Sort[{3, 2, 1}, False &] (* {1, 2, 3} *)
$\endgroup$
1
  • $\begingroup$ Thank you for clarifying my question. Sorry for the fundamental mistake of me. Thank you very much! $\endgroup$
    – Keyspire
    Commented Jun 14, 2023 at 18:09

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.