![](https://secure.gravatar.com/avatar/1f61c63c4a8c613367a9530e5994ef63.jpg?s=120&d=mm&r=g)
On Fri, Jul 20, 2012 at 11:44 AM, Mark Adam <dreamingforward@gmail.com>wrote:
On Fri, Jul 20, 2012 at 11:23 AM, Oleg Broytman <phd@phdru.name> wrote:
On Fri, Jul 20, 2012 at 06:11:52PM +0200, David Townshend < aquavitae69@gmail.com> wrote:
I was quite surprised to find that nobody seems to have suggested this before, because it seems like an obvious idea. Basically, add a special method __sort__ which, if specified, is used when sorted() is called. I can think of two immediate use cases:
1. When an object wants sorted() to return something other than a list, e.g. dict.__sort__ could return an OrderedDict. 2. When there is a more efficient method of sorting a specific sequence. E.g. sorting a range object should be trivial.
Is there some obvious reason why nobody has suggested this before? Is it worth pursuing?
Because it's too application-specific?
What are you talking about. This would solve the issue of ordering for dicts that became contentious a few years ago. If people don't like the arbitrary ordering, they can subclass it.
Sorry, I'll tone down my words. But this makes more sense than sort() as a built-in. In theory, sort is for collections, not just any object. So it could belong to an abstract collections base type. This would allow users to select lexical orderings, for example, rather than ASCII. It seems strange that no one thought of it before(it that true?).
mark