Two small examples of sorting in Python. The first example shows you how to sort a list of numeric strings as strings or as integers. The results are quite different ...
# sort a list of numeric strings:
list1 = ['0', '100', '28', '39', '1', '1003']
print "Original list of numeric strings:"
print list1
print "Sort as strings:"
list1.sort()
print list1 # ['0', '1', '100', '1003', '28', '39']
print "Sort as integers (Python24):"
list1.sort(key=int)
print list1 # ['0', '1', '28', '39', '100', '1003']
The second examples shows you how to sort two related lists in parallel, so the relationships stay intact ...
# sort two lists in parallel ...
# create two lists of same length
list1 = ['one', 'two', 'three', 'four']
list2 = ['uno', 'dos', 'tres', 'cuatro']
# create a list of tuples, (english, spanish) pairs
data = zip(list1, list2)
print data # [('one', 'uno'), ('two', 'dos'), ('three', 'tres'), ('four', 'cuatro')]
# can use parallel looping to display result ...
for english, spanish in data:
print english, spanish # one uno etc.
# each whole tuple will be sorted
data.sort()
print data # [('four', 'cuatro'), ('one', 'uno'), ('three', 'tres'), ('two', 'dos')]
# if you need two lists again after sorting then use ...
list3, list4 = map(lambda t: list(t), zip(*data))
print list3 # ['four', 'one', 'three', 'two']
print list4 # ['cuatro', 'uno', 'tres', 'dos']
# if you need just two tuples use ...
tuple1, tuple2 = zip(*data)
print tuple1 # ('four', 'one', 'three', 'two')
print tuple2 # ('cuatro', 'uno', 'tres', 'dos')