Skip to content

Commit 2882300

Browse files
committed
-
1 parent 8e98214 commit 2882300

File tree

2 files changed

+8
-2
lines changed
  • python_toolbox/nifty_collections
  • test_python_toolbox/test_nifty_collections/test_ordered_dict

2 files changed

+8
-2
lines changed

python_toolbox/nifty_collections/ordered_dict.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
See its documentation for more information.
88
'''
99

10+
import __builtin__
1011
from UserDict import DictMixin
1112

1213
from python_toolbox import comparison_tools
@@ -162,7 +163,7 @@ def move_to_end(self, key, last=True):
162163
end[2] = first[1] = link
163164

164165

165-
def sort(self, key=None):
166+
def sort(self, key=None, reversed=False):
166167
'''
167168
Sort the items according to their keys, changing the order in-place.
168169
@@ -172,7 +173,8 @@ def sort(self, key=None):
172173
key_function = \
173174
comparison_tools.process_key_function_or_attribute_name(key)
174175
sorted_keys = sorted(self.keys(), key=key_function)
175-
for key_ in sorted_keys[1:]:
176+
step = -1 if reversed else 1
177+
for key_ in sorted_keys[1::step]:
176178
self.move_to_end(key_)
177179

178180

test_python_toolbox/test_nifty_collections/test_ordered_dict/test.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ def test_sort():
4141
second_ordered_dict.sort('imag')
4242
assert second_ordered_dict == \
4343
OrderedDict(((3+1j, 'a'), (1+2j, 'b'), (2+3j, 'c')))
44+
45+
second_ordered_dict.sort('real', reversed=True)
46+
assert second_ordered_dict == \
47+
OrderedDict(((3+1j, 'a'), (2+3j, 'c'), (1+2j, 'b')))
4448

4549

4650

0 commit comments

Comments
 (0)