12

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|> *)
Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371
Alan
  • 13,686
  • 19
  • 38

1 Answers1

7

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

Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371