I have a text file that looks a bit like:
random text random text, can be anything blabla %A blabla
random text random text, can be anything blabla %D blabla
In [1]: def grp(pat, txt):
...: r = re.search(pat, txt)
...: return r.group(0) if r else '&'
In [2]: y
Out[2]:
['random text random text, can be anything blabla %A blabla',
'random text random text, can be anything blabla %D blabla',
'random text random text, can be anything blabla blabla %F',
'random text random text, can be anything blabla blabla',
'random text random text, %C can be anything blabla blabla']
In [3]: y.sort(key=lambda l: grp("%\w", l))
In [4]: y
Out[4]:
['random text random text, can be anything blabla %A blabla',
'random text random text, %C can be anything blabla blabla',
'random text random text, can be anything blabla %D blabla',
'random text random text, can be anything blabla blabla %F',
'random text random text, can be anything blabla blabla']