storage: sort results using a key, not a cmp function (#36515)

This commit is contained in:
Frédéric Péters 2019-11-12 11:40:52 +01:00
parent 0e968f3b83
commit 2927c87edd
1 changed files with 4 additions and 4 deletions

View File

@ -272,15 +272,15 @@ class StorableObject(object):
# only list can be sorted
objects = list(objects)
if order_by == 'id':
cmp_function = lambda x, y: cmp(lax_int(x.id), lax_int(y.id))
key_function = lambda x: lax_int(x.id)
elif order_by == 'name':
# proper collation should be done but it's messy to get working
# on all systems so we go the cheap and almost ok way.
from .misc import simplify
cmp_function = lambda x, y: cmp(simplify(x.name), simplify(y.name))
key_function = lambda x: simplify(x.name)
else:
cmp_function = lambda x, y: cmp(getattr(x, order_by), getattr(y, order_by))
objects.sort(cmp_function)
key_function = lambda x: getattr(x, order_by)
objects.sort(key=key_function)
if reverse:
objects.reverse()
if limit or offset: