[Python-Dev] PEP 265 - Sorting Dictionaries by Value

Grant Griffin grant.griffin@iowegian.com
Wed, 22 Aug 2001 08:32:57 -0500

At 08:43 AM 8/21/2001 +0200, Peter Funk wrote:
>Grant Griffin:
> > PEP: 265
> > Title: Sorting Dictionaries by Value
> > 1) Is this useful enough to justify the feature/code bloat?  (Tim has
> > suggested that this might be a hard sell to Guido, so if you like it in
> > whatever form, let's hear from you!)
>I think, this is feature bloat.  IMHO using dictionaries as counters
>for something is only one very special application of this generic
>basic data structure.
>A standard library module containing a class derived from
>dictionary should be able to fullfill this 'batteries included' wish.
>The documentation of the builtin type dictionary could contain
>pointers to useful derived classes, so that newbies seeking such
>a feature have a better chance to find it.

That's not a bad idea, and if that's how this turns out, I will think it 
had turned out well.

However, the problem I see with a class-based approach is that this is such 
a small thing that I'm not even sure how to write a class for it that isn't 
more grandiose than it deserves.  (Tim's suggestion might help here by 
expanding the scope of the idea <wink>.)  In comparison, the simplest of my 
proposals--the one I'm favoring at the moment--is just to add a boolean 
parameter to items:


This basically avoids the need for the tuple swap:

         >>> items = [(v, k) for (k, v) in items]

and it's somewhat faster in the sort-by-values case, because the tuples are 
built in the right order rather than being built in the wrong order, then 

So, in terms of feature bloat, we have one new parameter.  And in terms of 
code bloat, we have what I estimate to be 4-15 lines of C (depending on 
whether you optimize on speed or size.)

I guess my sense is that the bloat is small enough to be worth it.