with open(r'foo.txt', 'r') as f:text = f.read() lines = sorted(text.splitlines(), cmp=(lambda a, b: ord(a[4])-ord(b[4]) if a[4]!=b[4] else 1 if (a.split()[2]>b.split()[2]) else -1))