I have a data structure such that:
[(1, '(A) Begin plans for world domination'),
(2, 'Listen to Symphony for the New World'),
(3, '(A) Change world'),
(4, '(D) Listen to Symphony for the New World'),
(5, '(C) Hello, World!'), (4, 'Improve Python todo project'),
(6, 'Begin plans for world domination'),
(7, '(A) Improve Python todo project')]
And I would like to sort first by the contents of the parentheses (if they have parentheses), and then by number.
I'm currently filtering for keywords which works fine, my sort is incorrect:
filtered_items = [
(i+1, line.strip()) for i, line in enumerate(items)
if not args.filter_string or args.filter_string.lower() in line.lower()
]
sorted_items = sorted(filtered_items, key=lambda item: (
not item[1].startswith('('),
item[1][:3],
item[0],
))
this current sort will yield the output:
1 (A) Begin plans for world domination
3 (A) Change world
7 (A) Improve Python todo project
5 (C) Hello, World!'), (4, 'Improve Python todo project
4 (D) Listen to Symphony for the New World
6 Begin plans for world domination
2 Listen to Symphony for the New World
The last 2 are the wrong way round.
Can anyone explain why this is?