3

Say we have a list:

{{1/2, -(Sqrt[3]/2)}, {1, 0}, {1/2, Sqrt[3]/2}, {-(1/2), Sqrt[3]/2}, {-1, 0}, {-(1/2), -(Sqrt[3]/2)}}

now we want to sort that list by looking at the second component of each sub list (call it y coordinate for ease), i.e. sort it s.t. y is in the descending order. So we'd get

{{1/2, Sqrt[3]/2},{-1/2, Sqrt[3]/2},{1,0},{-1,0},{1/2,-(Sqrt[3]/2)},{-1/2,-(Sqrt[3]/2)}}

And yeah, I should mention that if there are more than one sub lists with equal y, we would additionally (sub)sort them by descending x (first component in each of the sub lists).

I tried to use SortBy, in different forms, but I don't seem to be able to figure it out by myself. I'd appreciate any help.

amator2357
  • 1,354
  • 5
  • 13

3 Answers3

1

As suggested in the comments, the following works as desired:

Reverse[SortBy[list, N[Last[#]]&]].

amator2357
  • 1,354
  • 5
  • 13
1
list = 
  {{1/2, -(Sqrt[3]/2)}, {1, 0}, {1/2, Sqrt[3]/2}, 
  {-(1/2), Sqrt[3]/2}, {-1, 0}, {-(1/2), -(Sqrt[3]/2)}};

Using ReverseSortBy (new in 12.0)

ReverseSortBy[N @* Last] @ list

{{1/2, Sqrt[3]/2}, {-1/2, Sqrt[3]/2}, {1, 0}, {-1, 0}, {1/2, -1/2 Sqrt[3]}, {-1/2, -1/2 Sqrt[3]}}

eldo
  • 67,911
  • 5
  • 60
  • 168
1
l = 
  {{1/2, -(Sqrt[3]/2)}, {1, 0}, {1/2, Sqrt[3]/2}, 
  {-(1/2), Sqrt[3]/2}, {-1, 0}, {-(1/2), -(Sqrt[3]/2)}};

The ordering using OrderingBy:

o = Flatten[RotateRight[Sort /@ Partition[Reverse@OrderingBy[#, Abs], 2]]] &@l;

Apply the ordering:

l[[o]]

{{-(1/2), Sqrt[3]/2}, {-(1/2), -(Sqrt[3]/2)}, {1, 0}, {-1, 0}, {1/2, -(Sqrt[3]/2)}, {1/2, Sqrt[3]/2}}

E. Chan-López
  • 23,117
  • 3
  • 21
  • 44