Unless I'm confused, you don't need a cmp *or* key function for that.
Sort works just fine on tuples of numbers. Is the following code what
you had in mind? It prints "True", meaning the cmp function didn't do
anything beyond sorted(). :)

import random
def cmp(A,B):
    if A[0] < B[0]:
        return -1
    elif A[0] > B[0]:
        return 1
    elif A[1] < B[1]:
        return -1
    elif A[1] > B[1]:
        return 1
    else:
        return 0
data = [(random.randint(0, 1000), random.randint(0, 1000))
        for _ in range(500)]
print sorted(data, cmp=cmp) == sorted(data)

On Tue, Nov 10, 2009 at 10:11 PM, Randolph Bentson
<[email protected]> wrote:
>
> I just noticed a significant change to the definition of the sort method on
> lists.  I've found the cmp function argument to sort very useful on occasion,
> so I'm astonished to learn it's been dropped in Python 3.  I can appreciate
> the utility of the key function (when it can be expressed), but sometimes
> it's impractical to assign each list element a stand-alone numerical value
> to be use for the built-in comparison.
>
> It's possible I've got it wrong. Can someone show me how to sort a list
> of tuples where two elements are numbers over an unknown range and the
> cmp function would be:
>        def cmp(A,B):
>          if A[0] < B[0]:
>            return -1
>          elif A[0] > B[0]:
>            return 1
>          elif A[1] < B[1]:
>            return -1
>          elif A[1] > B[1]:
>            return 1
>          else:
>            return 0
>
> --
> Randolph Bentson
> [email protected]
>



-- 
Gary
http://blog.extracheese.org

Reply via email to