2
$\begingroup$

How can I SortBy a list without final cannonical ordering? E.g. SortBy[{9, 1, 2, 5, 3, 7}, # Boole[# >= 6] & ] returns {1, 2, 3, 5, 7, 9} but I want {1, 2, 5, 3, 7, 9}.

$\endgroup$
9
  • 1
    $\begingroup$ This is surely a dupe, but the way to ensure stable sorting in this situation is to do SortBy[{9, 1, 2, 5, 3, 7}, {# Boole[# >= 6] &}]. $\endgroup$ – J. M.'s ennui Mar 29 '20 at 14:51
  • 1
    $\begingroup$ Why does this work? I am unable to find a documentaton about this structure. $\endgroup$ – Al Guy Mar 29 '20 at 15:00
  • 2
    $\begingroup$ This great answer by Andrew Moylan, to the SO question Stable Sorting, ie, Minimally-Disruptive Sorting, may be of interest $\endgroup$ – user1066 Mar 29 '20 at 15:01
  • $\begingroup$ @user1066 Clear explanation. Thanks! $\endgroup$ – Al Guy Mar 29 '20 at 15:04
  • $\begingroup$ Ah, I found this. Yes, the documentation could mention this; I encourage you to send that suggestion to Support. $\endgroup$ – J. M.'s ennui Mar 29 '20 at 15:05

Your Answer

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

Browse other questions tagged or ask your own question.