I'm trying to sort the below list of list:
points = [[1, 4, 'start'], [1, 6, 'start'], [2, 8, 'end'], [2, 5, 'end'], [3, 4, 'end'], [3, 9, 'start']]
Expected output:
[[1, 6, 'start'], [1, 4, 'start'], [2, 5, 'end'], [2, 8, 'end'], [3, 9, 'start'], [3, 4, 'end']]
Conditions for sorting are:
If p1[0] == p2[0] and p1[2] == 'start' and p2[2] == 'start', then p1[1] or p2[1] with greater value should come first.
If p1[0] == p2[0] and p1[2] == 'end' and p2[2] == 'end', then p1[1] or p2[1] with lesser value should come first.
If p1[0] == p2[0] and p1[2] == 'start' and p2[2] == 'end', then point with 'start' should come first.
I tried to write a custom comparator (getting correct answer), just wondering is this the right approach? Can it be more simpler?
def mycmp(p1, p2):
if p1[0] < p2[0]:
return -1
if p1[0] == p2[0]:
if p1[2] == 'start' and p2[2] == 'start' and p1[1] > p2[1]:
return -1
elif p1[2] == 'end' and p2[2] == 'end' and p1[1] < p2[1]:
return -1
elif p1[2] == 'start' and p2[2] == 'end':
return -1
return 0
points.sort(key=cmp_to_key(mycmp))
1? That doesn't seem quite right for a sort callback.