p = re.compile("""^(?P[A-Z]{1,3}) (?P\\d{1,4})(\\ ?) (\\.(?P\\d{1,3}))? (?P\\ [A-Za-z0-9]{1,4}\\ )? ([\\ \\.](?P[A-Z][0-9]{1,4})) (\\ (?P[A-Za-z0-9]{0,4}))? (\\.?(?P[A-Z][0-9]{1,4}))? (\\ (?P\\w*)\\ ?)? (\\ (?P\\w*)\\ ?)? (\\ (?P\\w*)\\ ?)?""", re.VERBOSE) def ncmp(x,y): if x is None: return -1 if y is None: return 1 x = int(x) y = int(y) return cmp(x,y) def sortfunc(x, y): xp = p.search(x) yp = p.search(y) parts = {'aclass':cmp,'nclass':ncmp,'dclass':ncmp,'date':cmp, 'c1':cmp,'c1d':cmp,'c2':cmp,'e8':cmp,'e9':cmp,'e10':cmp} for part in parts: cr = parts[part](xp.group(part),yp.group(part)) if cr != 0: return cr