
Python 201: How to sort a dictionary by value - driscollis
http://www.blog.pythonlibrary.org/2012/06/19/python-201-how-to-sort-a-dictionary-by-value/
======
a_bonobo
Why on earth would you use a dictionary inside a dictionary when you can just
put everything into a class?

Easier to read, easier to loop over, easier to sort, easier to change, easier
to debug.

~~~
jimmytucson
The example data sorta looks like a MongoDB collection.

I kinda err towards using basic data structures rather than whipping out my
own class but I'm not sure that's valid. What would your key-value store class
look like?

~~~
a_bonobo
Depends how you want to sort, or how accessible you want your data to be.

Easiest would be something like:

{user1-name:user1-object, user2-name:user2-object...}

You could use a list but look-up is O(1) in dictionaries compared to O(n) in
lists.

And if you then want to sort by any value of the user-object, you just hand
the relevant gets-method as sorting-key, e.g. sorted(dictionary,
key=user.getAddress())

