alok shreevathsa
alok shreevathsa

Reputation: 57

How to sort array in python?

This is my code,

from array import array
def genId():
    ma={'0':1001,'1':1002,'3':1003,'4':1004,'5':1005,'6':1006,'7':1007}
    print ma
    n=array('i')
    for j in ma:
        n.append(ma[j])
    print n
if __name__=="__main__":
  genId()

I want to sort the values of n. How to do it? and this is the output i'm getting,

{'1': 1002, '0': 1001, '3': 1003, '5': 1005, '4': 1004, '7': 1007, '6': 1006}
array('i', [1002, 1001, 1003, 1005, 1004, 1007, 1006])

please tell me how to sort these array values? thanks in advance..

Upvotes: 0

Views: 373

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1121744

Sort the values of ma before adding them to the array:

for value in sorted(ma.values()):
    n.append(value)

Upvotes: 1

Related Questions