def k(line):
    v = line.partition('%')[2]
v = v[0] if v else 'z'
return v
print(''.join(sorted(open('data.txt', 'rb'), key=k)))
Sort a list of strings based on regular expression match or something similar