How to sort higher to lower in Python
Question: Hi i need to **sort** a int value in **forloop** and i am running this on
terminal, below is the code

    
    
    match = myworld.objects.filter(series=1)
    for i in match:
        print i.w
    

which results like

    
    
    34
    32
    24
    39
    32
    33
    36
    33
    23
    34
    38
    38
    32
    31
    30
    34
    30
    30
    31
    36
    31
    35
    33
    43
    

please tell me how can i sort them so that i can get a **lower** to **higher**
or higher to lower thanks


Answer: for lower to higher

    
    
    from operator import attrgetter
    for i in sorted(match, key=attrgetter('w')):
    

for higher to lower

    
    
    for i in sorted(match, key=attrgetter('w'), reverse=True):
    

If you don't need the objects, you can also just sort the attribute you are
interested in

    
    
    for i in sorted(x.w for x in match):
    

