diff --git a/django/template/defaultfilters.py b/django/template/defaultfilters.py
index 1500a05..1606dad 100644
a
|
b
|
def dictsort(value, arg):
|
479 | 479 | Takes a list of dicts, returns that list sorted by the property given in |
480 | 480 | the argument. |
481 | 481 | """ |
482 | | var_resolve = Variable(arg).resolve |
483 | | decorated = [(var_resolve(item), item) for item in value] |
484 | | decorated.sort() |
485 | | return [item[1] for item in decorated] |
| 482 | return sorted(value, key=Variable(arg).resolve) |
486 | 483 | dictsort.is_safe = False |
487 | 484 | |
488 | 485 | def dictsortreversed(value, arg): |
… |
… |
def dictsortreversed(value, arg):
|
490 | 487 | Takes a list of dicts, returns that list sorted in reverse order by the |
491 | 488 | property given in the argument. |
492 | 489 | """ |
493 | | var_resolve = Variable(arg).resolve |
494 | | decorated = [(var_resolve(item), item) for item in value] |
495 | | decorated.sort() |
496 | | decorated.reverse() |
497 | | return [item[1] for item in decorated] |
| 490 | return sorted(value, key=Variable(arg).resolve, reverse=True) |
498 | 491 | dictsortreversed.is_safe = False |
499 | 492 | |
500 | 493 | def first(value): |
diff --git a/tests/regressiontests/defaultfilters/tests.py b/tests/regressiontests/defaultfilters/tests.py
index 27014a7..98deb8e 100644
a
|
b
|
class DefaultFiltersTests(unittest.TestCase):
|
260 | 260 | u'some html with alert("You smell") disallowed tags') |
261 | 261 | |
262 | 262 | def test_dictsort(self): |
263 | | sorted_dicts = dictsort([{'age': 23, 'name': 'Barbara-Ann'}, |
264 | | {'age': 63, 'name': 'Ra Ra Rasputin'}, |
265 | | {'name': 'Jonny B Goode', 'age': 18}], 'age') |
266 | | |
| 263 | dicts = [{'age': 23, 'name': 'Barbara-Ann'}, |
| 264 | {'age': 63, 'name': 'Ra Ra Rasputin'}, |
| 265 | {'age': 18, 'name': 'Jonny B Goode'}] |
| 266 | sorted_dicts = dictsort(dicts, 'age') |
267 | 267 | self.assertEqual([sorted(dict.items()) for dict in sorted_dicts], |
268 | 268 | [[('age', 18), ('name', 'Jonny B Goode')], |
269 | 269 | [('age', 23), ('name', 'Barbara-Ann')], |
270 | 270 | [('age', 63), ('name', 'Ra Ra Rasputin')]]) |
| 271 | self.assertEqual(dicts, [{'age': 23, 'name': 'Barbara-Ann'}, |
| 272 | {'age': 63, 'name': 'Ra Ra Rasputin'}, |
| 273 | {'age': 18, 'name': 'Jonny B Goode'}]) |
271 | 274 | |
272 | 275 | def test_dictsortreversed(self): |
273 | | sorted_dicts = dictsortreversed([{'age': 23, 'name': 'Barbara-Ann'}, |
274 | | {'age': 63, 'name': 'Ra Ra Rasputin'}, |
275 | | {'name': 'Jonny B Goode', 'age': 18}], |
276 | | 'age') |
277 | | |
| 276 | dicts = [{'age': 23, 'name': 'Barbara-Ann'}, |
| 277 | {'age': 63, 'name': 'Ra Ra Rasputin'}, |
| 278 | {'age': 18, 'name': 'Jonny B Goode'}] |
| 279 | sorted_dicts = dictsortreversed(dicts, 'age') |
278 | 280 | self.assertEqual([sorted(dict.items()) for dict in sorted_dicts], |
279 | 281 | [[('age', 63), ('name', 'Ra Ra Rasputin')], |
280 | 282 | [('age', 23), ('name', 'Barbara-Ann')], |
281 | 283 | [('age', 18), ('name', 'Jonny B Goode')]]) |
| 284 | self.assertEqual(dicts, [{'age': 23, 'name': 'Barbara-Ann'}, |
| 285 | {'age': 63, 'name': 'Ra Ra Rasputin'}, |
| 286 | {'age': 18, 'name': 'Jonny B Goode'}]) |
282 | 287 | |
283 | 288 | def test_first(self): |
284 | 289 | self.assertEqual(first([0,1,2]), 0) |