[Python-3000] sort vs order (was: What should the focus for 2.6 be?) (original) (raw)

Michael Chermside mcherm at mcherm.com
Thu Aug 24 23:45:24 CEST 2006


Jim Jewett writes:

Given an arbitrary collection of objects, I want to be able to order them in a consistent manner, at least within a single interpreter session.

I think this meets your specifications:

myList = [2.5, 17, object(), 3+4j, 'abc'] myList.sort(key=id)

I prefer Guido's suggestion (id=lambda x: (type(x), x)), but it doesn't handle types that are not comparable (like the complex number I included to be perverse). Frankly, I don't know why you have an "arbitrary collection of objects" -- the only things I have ever dealt with that handled truly arbitrary collections of objects were garbage collectors and generic caching mechanisms. In either case you really wouldn't care how things sorted so long as it was consistant, and then sorting by id works nicely.

Of course, I doubt this is what you're doing because if you REALLY had arbitrary objects (including uncomparable things like complex numbers) then you would already need to be doing this today and your code wouldn't even need to be modified when you upgraded to 3.0.

-- Michael Chermside



More information about the Python-3000 mailing list