Commit fa735988 authored by Yoshinori Okuji's avatar Yoshinori Okuji

Implement a generic version of sort function. Remove sortOnId, as it is not used.


git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@4096 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent 651dfac5
......@@ -65,8 +65,60 @@ from Accessor.TypeDefinition import *
# Generic sort method
#####################################################
def sortOnId(sort_id):
return lambda a,b: cmp(a[1].getProperty(sort_id), b[1].getProperty(sort_id))
def sortValueList(value_list, sort_on=None, sort_order=None, **kw):
"""Sort values in a way compatible with ZSQLCatalog.
"""
if sort_on is not None:
if type(sort_on) == type(''):
sort_on = (sort_on,)
reverse = (sort_order in ('descending', 'reverse', 'DESC'))
new_sort_on = []
for key in sort_on:
if type(key) == type(''):
new_sort_on.append((key, reverse, None))
else:
if len(key) == 1:
new_sort_on.append((key[0], reverse, None))
elif len(key) == 2:
new_sort_on.append((key[0],
key[1] in ('descending', 'reverse', 'DESC'),
None))
else:
# Emulate MySQL types
as_type = key[2].lower()
if as_type in ('int', 'bigint'):
f=int
elif as_type in ('float', 'real', 'double'):
f=float
else:
# XXX: For an unknown type, use a string.
f=str
new_sort_on.append((key[0],
key[1] in ('descending', 'reverse', 'DESC'),
f))
sort_on = new_sort_on
def sortValues(a, b):
result = 0
for key, reverse, as_type in sort_on:
x = a.getProperty(key, None)
y = b.getProperty(key, None)
if as_type is not None:
try:
x = as_type(x)
y = as_type(y)
except TypeError:
pass
result = cmp(x, y)
if reverse:
result = -result
if result != 0:
break
return result
value_list.sort(sortValues)
return value_list
#####################################################
# Useful methods
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment