I have a pandas DataFrame constructed like so:
l1 = [[[1,2], [3,5]], [[3,2], [5,2]]]
l2 = [[[9,2], [4,5]], [[4,3], [2,7]]]
l3 = [[[3,3], [4,4]], [[6,3], [4,6]]]
l4 = [[[4,4], [3,5]], [[2,5], [3,4]]]
small_df = pd.DataFrame({'col': [l1, l2, l3, l4]})
print(small_df)
col
0 [[[1, 2], [3, 5]], [[3, 2], [5, 2]]]
1 [[[9, 2], [4, 5]], [[4, 3], [2, 7]]]
2 [[[3, 3], [4, 4]], [[6, 3], [4, 6]]]
3 [[[4, 4], [3, 5]], [[2, 5], [3, 4]]]
I would like to first sort the inner list of points so that [[9, 2], [4, 5]] becomes [[4, 5], [9, 2]], then sort the outer list so that [[[4, 5], [9, 2]], [[2, 7], [4, 3]]] becomes [[[2, 7], [4, 3]], [[4, 5], [9, 2]]]. I have figure out how to sort the outer list like so:
small_df["sorted"] = small_df["col"].apply(sorted)
small_df
col sorted
0 [[[1, 2], [3, 5]], [[3, 2], [5, 2]]] [[[1, 2], [3, 5]], [[3, 2], [5, 2]]]
1 [[[9, 2], [4, 5]], [[4, 3], [2, 7]]] [[[4, 3], [2, 7]], [[9, 2], [4, 5]]]
2 [[[3, 3], [4, 4]], [[6, 3], [4, 6]]] [[[3, 3], [4, 4]], [[6, 3], [4, 6]]]
3 [[[4, 4], [3, 5]], [[2, 5], [3, 4]]] [[[2, 5], [3, 4]], [[4, 4], [3, 5]]]
How can I sort the inner list first though? I would like to end up with this:
col goal
0 [[[1, 2], [3, 5]], [[3, 2], [5, 2]]] [[[1, 2], [3, 5]], [[3, 2], [5, 2]]]
1 [[[9, 2], [4, 5]], [[4, 3], [2, 7]]] [[[2, 7], [4, 3]], [[4, 5], [9, 2]]]
2 [[[3, 3], [4, 4]], [[6, 3], [4, 6]]] [[[3, 3], [4, 4]], [[4, 6], [6, 3]]]
3 [[[4, 4], [3, 5]], [[2, 5], [3, 4]]] [[[2, 5], [3, 4]], [[3, 5], [4, 4]]]
Ideally, I would like to do all the sorting at the same time so it's as efficient as possible.