faqts : Computers : Programming : Languages : Python : Common Problems : Lists

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

5 of 5 people (100%) answered Yes
Recently 5 of 5 people (100%) answered Yes

Entry

How to sort a list 1.8.txt,1.2.txt,1.9.txt,1.10.txt => 1.2.txt,1.8.txt,1.9.txt,1.10.txt

Feb 15th, 2004 10:02
Washington Corrêa, Sam G,


Here it goes a function that can sort this list:
def sortIt(l):
  p = lambda s: s.strip('.txt').split('.') + [s]
  h = lambda x: [int(x[0]), int(x[1]), x[2]]
  l = map(h, map(p, l))
  l.sort()
  return map(lambda k: k[2], l)
Example:
m = ['1.8.txt', '1.2.txt', '1.9.txt', '1.10.txt']
print sortIt(m)
ok?
Junior