I am trying to sort a two-dimensional list while prioritizing sorting along one of the dimensions.
Example:
twod_list = [[116.2,103.4],[124.9,103.4],[129.5,103.4],
[144.6,103.4],[148.9,103.4],
[119.1,109.3],[125.9,103.4],[146.3,109.3],[135.8,103.4]]
I am trying to sort this in the following order, first sort along the second dimension, then sort along the first dimension while maintaining the order on the second dimension. So my expected sorted list looks like below
sorted_twod_list = [[116.2,103.4],[124.9,103.4],[125.9,103.4],[129.5,103.4],[135.8,103.4],
[144.6,103.4],[148.9,103.4],
[119.1,109.3],[146.3,109.3]]
I tried to sort first along the second dimension then on the first dimension but in doing so it is changing the order.
twod_list = [[116.2,103.4],[124.9,103.4],[129.5,103.4],
[144.6,103.4],[148.9,103.4],
[119.1,109.3],[124.9,103.4],[146.3,109.3],[135.8,103.4]]
twod_sorted_on_y = sorted(twod_list,key=lambda l:l[1])
twod_sorted = sorted(twod_sorted_on_y,key=lambda l:l[0])
I am getting the following result:
twod_sorted = [[116.2, 103.4],[119.1, 109.3],[124.9, 103.4],
[124.9, 103.4],[129.5, 103.4],
[135.8, 103.4],[144.6, 103.4],[146.3, 109.3],[148.9,103.4]]
We can also accomplish this using if condition, but I want to know whether any short cut exists using numpy or pandas?