Python - How to special sort list
Question: can anyone advise me how to sort the array by column 'age'?

    
    
    student = {
         0 : {'name' : 'jane', 'age' : 40},
         1 : {'name' : 'pool', 'age' : 11},
         2 : {'name' : 'dave', 'age' : 28}
    }
    
    print(student[0])
    print(student[1])
    print(student[2])
    

results screen

    
    
    {'name': 'jane', 'age': 40}
    {'name': 'pool', 'age': 11}
    {'name': 'dave', 'age': 28}
    

I tried

    
    
    student = sorted(student, key=lambda student: student[2]) # sort by age not work
    

But it does not work nothing :-( Thank you for your help

\-- EDIT -- Correct sort list (sorting age)

    
    
    print(student[0])
    print(student[1])
    print(student[2])
    

results screen

    
    
    {'name': 'pool', 'age': 11}    
    {'name': 'dave', 'age': 28}
    {'name': 'jane', 'age': 40}
    


Answer: These are all `dict`s, not `list`s, so they don't have order; for the inner
`dict`s, they don't have numerical keys, so indexing them is an error. That
said, you can convert the outer `dict` to a sorted `list` to get the result
you want:

    
    
    from operator import itemgetter
    
    students = {
        0: {'name': 'jane', 'age': 40},
        1: {'name': 'pool', 'age': 11},
        2: {'name': 'dave', 'age': 28}
    }
    
    # Sort the sub-dicts by age and print
    for student in sorted(students.values(), key=itemgetter('age')):
        print(student)
    

Note: Because the sub-`dict`s are also unordered, there is no guarantee that
`name` appears before `age` in the output for each student. But this will
output the students in the correct order.

