Skip to content

Commit

Permalink
Fix reversed sorting by multiple index + limit (#120)
Browse files Browse the repository at this point in the history
Fix reversed sorting by multiple index by forcing the ``_sort_iterate_resultset`` sorting method when we have more than one sorting index

Fixes #108
  • Loading branch information
ale-rt authored Mar 29, 2021
1 parent de397aa commit 5a3272f
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ Changelog

- Fix case where multiple indexes with similar name seperated by ``_`` were interpreted as options.
(`#78 <https://github.com/zopefoundation/Products.ZCatalog/issues/78>`_)
- Fix reversed sorting by multiple index by forcing the ``_sort_iterate_resultset``
sorting method when we have more than one sorting index
(`#108 <https://github.com/zopefoundation/Products.ZCatalog/issues/108>`_)


6.0 (2020-10-08)
Expand Down
2 changes: 1 addition & 1 deletion src/Products/ZCatalog/Catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -1000,7 +1000,7 @@ def sortResults(self, rs, sort_index,
# Choose one of the sort algorithms.
if iterate_sort_index:
sort_func = self._sort_iterate_index
elif limit is None or (limit * 4 > rlen):
elif limit is None or (limit * 4 > rlen) or sort_index_length > 1:
sort_func = self._sort_iterate_resultset
elif first_reverse:
sort_func = self._sort_nbest
Expand Down
22 changes: 22 additions & 0 deletions src/Products/ZCatalog/tests/test_catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -739,6 +739,28 @@ def test_sort_on_two_reverse(self):
for x in range(upper - 1):
self.assertTrue(a[x].num > a[x + 1].num)

def test_sort_on_two_reverse_with_limit(self):
catalog = self._make_one()
for num in range(-10, 0):
obj = Dummy(num)
obj.att1 = "att1foo"
obj.att2 = "att2"
catalog.catalogObject(obj, repr(num))
a = catalog(
att2='att2',
sort_on=('att1', 'num'),
sort_order='reverse',
sort_limit=20,
)
self.assertEqual(
[x.num for x in a[:10]],
[-1, -2, -3, -4, -5, -6, -7, -8, -9, -10],
)
self.assertEqual(
[x.num for x in a[10:]],
[99, 98, 97, 96, 95, 94, 93, 92, 91, 90],
)

def test_sort_on_two_reverse_neither(self):
catalog = self._make_one()
upper = self.upper
Expand Down

0 comments on commit 5a3272f

Please sign in to comment.