Message263367
I'm trying to sort a list of tuples. Most of the tuples are pairs of US state names. However, some of the tuples have None instead of the 2nd name. I want the items sorted first by the 1st element, and then by the 2nd element, BUT I want the None to count as LARGER than any name. Thus, I want to see [('Alabama', 'Iowa'), ('Alabama', None)] rather than [('Alabama', None), ('Alabama', 'Iowa')]. I defined the following comparitor:
def cmp_keys (k1, k2):
retval = cmp(k1[0], k2[0])
if retval == 0:
if k2[1] is None:
retval = -1
if k1[1] is None:
retval = 1
else:
retval = cmp(k1[1], k2[1])
return retval
However, once I sort using this, some of the elements are out of order. |
|
Date |
User |
Action |
Args |
2016-04-14 01:14:51 | David.Manowitz | set | recipients:
+ David.Manowitz |
2016-04-14 01:14:51 | David.Manowitz | set | messageid: <1460596491.57.0.479961799171.issue26751@psf.upfronthosting.co.za> |
2016-04-14 01:14:51 | David.Manowitz | link | issue26751 messages |
2016-04-14 01:14:50 | David.Manowitz | create | |
|