Open filak opened 1 year ago
Sure there is a workaround - pre-populate the data with sort_key() values. But this might not be always optimal/feasible.
from operator import itemgetter
from pyuca import Collator
coll = Collator()
def multisort_list_of_dicts(xs, specs):
for key, reverse in reversed(specs):
xs.sort(key=itemgetter(key), reverse=reverse)
return xs
data = [{'k1': 'd', 'k2': 10},{'k1': 'č', 'k2': 10},{'k1': 'a', 'k2': 20},{'k1': 'a', 'k2': 10}]
data_sortable = []
for d in data:
d['ks'] = coll.sort_key( d['k1'] )
data_sortable.append(d)
sort_spec = (('k2', False), ('ks', False))
for item in multisort_list_of_dicts(data_sortable, sort_spec):
print(item)
Output:
{'k1': 'a', 'k2': 10, 'ks': (7239, 0, 32, 0, 2, 0)}
{'k1': 'č', 'k2': 10, 'ks': (7290, 0, 32, 40, 0, 2, 2, 0)}
{'k1': 'd', 'k2': 10, 'ks': (7311, 0, 32, 0, 2, 0)}
{'k1': 'a', 'k2': 20, 'ks': (7239, 0, 32, 0, 2, 0)}
Thank you for a great library!
I would like to sort a list of dicts by multiple values - some possibly in Unicode - sample program:
Standard sorting:
Unicode sorting for k1:
this obviously cannot work, because sort_key() expects a string:
def sort_key(self, string):
How to get the desired output with pyuca ?
How to possibly handle the itemgetter input in pyuca ?