Complex python dictionary sort by value

I have a dictionary with ints for keys, and strings for values. I need to to sort by the strings, so that when I go dict.values() I get the sorted list.

The strings are values like this: 45_12_something_23

I need to sort numbers as numbers and strings as strings. A given row is guaranteed to be either a string or a number (not a mixture).

Whats a good way to do this in python? Performance isnt an issue.

Answers


Convert your dictionary to a list of (key,value) pairs. Sort them however you'd like. (Or is that your question - how to do that?) Then insert the sorted (key,value) pairs into a collections.OrderedDict, which will remember the insertion order and use the same order when iterating.

Note that you can't modify the dict you already have, this will make a new dict with the properties you want.


Need Your Help

How can I get an item in a specific row in a list from a database?

android sqlite listview

I have been searching through a lot of questions but they don't ask or answer what I'm looking for, so here is what I'm trying to do.

Layout suggestions for GUI?

java swing user-interface layout

So I want to make a new JList and a new JPanel at the bottom, but I'm not too familiar with the BoxLayout, FlowLayout and the like. What do you suggest so I can make my GUI turn into something like...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.