[New-bugs-announce] [issue26751] Possible bug in sorting algorithm

David Manowitz report at bugs.python.org
Wed Apr 13 21:14:51 EDT 2016


New submission from David Manowitz:

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.

----------
components: Interpreter Core
messages: 263367
nosy: David.Manowitz
priority: normal
severity: normal
status: open
title: Possible bug in sorting algorithm
type: behavior
versions: Python 2.7

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue26751>
_______________________________________


More information about the New-bugs-announce mailing list