12
$\begingroup$

Should I be able to use the "list of functions" approach to getting a stable sort of an association? In 11.1 I see,

assoc01 = <|10 -> 1, 8 -> 3, 9 -> 2, 7 -> 4|>;
SortBy[assoc01, Mod[#, 3] &]    (* <|8->3,7->4,10->1,9->2|> *)
SortBy[assoc01, {Mod[#, 3] &}]  (* <|10->1,9->2,8->3,7->4|> *)
$\endgroup$
3
  • 1
    $\begingroup$ I get the same result in 11.3 $\endgroup$
    – mikado
    Aug 10, 2018 at 20:28
  • 4
    $\begingroup$ I think this is a bug that should be reported to support. $\endgroup$
    – Carl Woll
    Aug 10, 2018 at 21:58
  • $\begingroup$ @CarlWoll OK thanks; reported. $\endgroup$
    – Alan
    Aug 11, 2018 at 2:15

1 Answer 1

7
$\begingroup$

Indeed I think you should, and I agree with Carl Woll that this should be reported.

In the meantime here is a work-around using a variation of my orderingBy from Retaining and reusing a one-to-one mapping from a sort:

aSortBy[a_Association, sfuns_List] :=
  a[[ SortBy[Range @ Length @ a, Cases[sfuns, f_ :> (f@a[[#]] &)]] ]]

aSortBy[a_, sfn_] := SortBy[a, sfn]

aSortBy[fns_][a_] := aSortBy[a, fns]

Test:

assoc01 = <|10 -> 1, 8 -> 3, 9 -> 2, 7 -> 4|>;

aSortBy[assoc01, Mod[#, 3] &]

aSortBy[assoc01, {Mod[#, 3] &}]
<|8 -> 3, 7 -> 4, 10 -> 1, 9 -> 2|>

<|8 -> 3, 10 -> 1, 7 -> 4, 9 -> 2|>
$\endgroup$

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.