I want to sort a dictionary, say that it was like this:

a_dict = {'2':12354, '1':12355432, '4':35342, '3':858743}

How would I be able to sort it so it's like this:

a_dict = {'1':12355432, '2':12354,  '3':85874, '4':35342}

Is there any algorithms already built it?
Or do i have to make one myself

any help would be appreciated :)

When take out data from a dictionary order is not important.
But here is a soultion.

>>> a_dict = {'2':12354, '1':12355432, '4':35342, '3':858743}
>>> sorted(a_dict.items(), key=lambda (v,k): (v,k))
[('1', 12355432), ('2', 12354), ('3', 858743), ('4', 35342)]

Ok so i have a file which i read in ... then i read the data line by line and create lists

basically i needed to organsise(sort the data, in numerical order which i can do no problem and when i do the print statement it all works... no how can i write this sorted data into a new file?

when i do i get loads of lines into my new with the final line having all my data in the sorted order??? its because i need to carry out further functions on the sorted data ..

any suggestions????

i can mail my codes if anyone can help( im trying to so the excel sort function to my data if that makes sense?)

thanks xx

Can you make a new post with same question sab786.
If you have som code or how output list look,post that.

It`s not good forum behavior in any forum to hijack thread with new question.

List values are accessed by their index.
Dictionary values are accessed by their key. To allow for high speed key searches, the keys are in a hash order. So a dictionary sorted alphabetically by it's keys would make no sense and would defeat the whole dictionary idea.

The best you can do is to present the contents of a dictionary the way snippsat did it, as a list of (key, value) tuples. Also as a table (sorted by key) in your program:

name_age = {'Joe': 18, 'Frank': 45, 'Udo': 56, 'Tom': 34}
# as a table sorted by name (key)
for name in sorted(name_age):
    print( "%-10s %d" % (name, name_age[name]) )

my output -->
Frank      45
Joe        18
Tom        34
Udo        56

sorry i did that as soon as i realised i thought i was posting a new thread

If you are using Python 3x, an upgrade to python 3.1 will get you the sorted dictionary type. Otherwise, ignore this post.