7

After long searching, I noticed that the SortBy[list,f] function has an annoying side effect: If the function f yields the same value for consecutive elements of the list, those are still sorted using the canonical ordering of the elements. This is very annoying - I want to sort only on the first element of a list but leave the order of list elements unchanged if the first element is not changing. How can this be achieved?

e.g.:

    y1 = {{1., 2.}, {1., 1.}, {3., 2.}}
    SortBy[y1, #[[1]] &]

Gives:

    {{1.,1.},{1.,2.},{3.,2.}}

But I would like to obtain:

    {{1.,2.},{1.,1.},{3.,2.}}

hippo3773
  • 1,246
  • 6
  • 10

1 Answers1

9
SortBy[y1, {#[[1]] &}]
{{1., 2.}, {1., 1.}, {3., 2.}}
kglr
  • 394,356
  • 18
  • 477
  • 896