diff --git a/product/ERP5Type/Utils.py b/product/ERP5Type/Utils.py index d74a8425db5c9c2b12721f318b4831e9e622233f..c7bd42fa332e7579be2502f057d55f7b70899567 100644 --- a/product/ERP5Type/Utils.py +++ b/product/ERP5Type/Utils.py @@ -144,6 +144,26 @@ else: # Generic sort method ##################################################### +class OrderableList(object): + def __init__(self, *args): + self.__slots__ = args + + def __lt__(self, other): + for i, v in enumerate(self.__slots__): + try: + other_v = other.__slots__[i] + except IndexError: + return False + c = cmp(v, other_v) + if c == -1: + return True + elif c == 1: + return False + return True + + def __repr__(self): + return repr(self.__slots__) + sort_kw_cache = {} def sortValueList(value_list, sort_on=None, sort_order=None, **kw): @@ -201,7 +221,10 @@ def sortValueList(value_list, sort_on=None, sort_order=None, **kw): except TypeError: pass value_list.append(x) - return value_list + if six.PY2: + return value_list + else: + return OrderableList(*value_list) sort_kw = {'key':sortValue, 'reverse':reverse} sort_kw_cache[(sort_on, sort_order)] = sort_kw else: diff --git a/product/ERP5Type/tests/testERP5Type.py b/product/ERP5Type/tests/testERP5Type.py index 1a3e554f17b47b35d4434eb7b51eac9def4b9111..26df6f4940185d5725e279b5e3223f4153cbb96e 100644 --- a/product/ERP5Type/tests/testERP5Type.py +++ b/product/ERP5Type/tests/testERP5Type.py @@ -38,7 +38,7 @@ from Acquisition import aq_base from Products.ERP5Type.tests.ERP5TypeTestCase import ERP5TypeTestCase from AccessControl.ZopeGuards import guarded_import from Products.ERP5Type.tests.utils import LogInterceptor, createZODBPythonScript -from Products.ERP5Type.Utils import cmp +from Products.ERP5Type.Utils import OrderableList, cmp class TestERP5Type(ERP5TypeTestCase, LogInterceptor): """ @@ -296,6 +296,16 @@ class TestERP5Type(ERP5TypeTestCase, LogInterceptor): self.assertEqual(cmp(None, ''), -1) self.assertEqual(cmp(0, ''), -1) + def test_OrderableList(self): + o1 = OrderableList(None, 1) + o2 = OrderableList(0, 0) + self.assertEqual(sorted([o1, o2]), [o1, o2]) + self.assertEqual(sorted([o2, o1]), [o1, o2]) + o3 = OrderableList(1,2,3) + o4 = OrderableList(1,2,3,4) + self.assertEqual(sorted([o3, o4]), [o3, o4]) + self.assertEqual(sorted([o4, o3]), [o3, o4]) + def test_suite(): suite = unittest.TestSuite() suite.addTest(unittest.makeSuite(TestERP5Type))