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|> *)
  • 1
    I get the same result in 11.3 – mikado Aug 10 at 20:28
  • 4
    I think this is a bug that should be reported to support. – Carl Woll Aug 10 at 21:58
  • @CarlWoll OK thanks; reported. – Alan Aug 11 at 2:15

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|>

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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