Let's say I have this list:
my_list = [
{'id': '[email protected]'}, {'id': '[email protected]'}, {'id': '[email protected]'}, {'id': '[email protected]'},
{'id': '[email protected]'}, {'id': '[email protected]'}, {'id': '[email protected]'}, {'id': '[email protected]'}
]
I want the resulting list to be sorted first by what precedes the @ and then by the version numbers.
Doing this: natsort.natsorted(my_list, key=itemgetter(*['id']), reverse=True)
I get the following result:
[{'id': '[email protected]'},
{'id': '[email protected]'},
{'id': '[email protected]'},
{'id': '[email protected]'},
{'id': '[email protected]'},
{'id': '[email protected]'},
{'id': '[email protected]'},
{'id': '[email protected]'}]
How do I get natsort to work so that all the items for "Air" come before all the items for "Bear", while still sorting also by version (as it's currently doing correctly)?
I would use a custom comparison function to reverse the comparison only on the version numbers, using
natsort.natsort_keygento make a function that will compare versions numbers correctly.Output: