Ticket #13828: 13828_dry_dictsort.diff
File 13828_dry_dictsort.diff, 1.4 KB (added by , 14 years ago) |
---|
-
defaultfilters.py
459 459 # LISTS # 460 460 ################### 461 461 462 def _dictsort(value, arg, reverse=False): 463 """ 464 Takes a list of dicts, returns that list sorted by the property given in 465 the argument. 466 The returned list will be in reverse order if `reverse` is `True`. 467 """ 468 var_resolve = Variable(arg).resolve 469 value.sort(key=lambda item: var_resolve(item), reverse=reverse) 470 return value 471 462 472 def dictsort(value, arg): 463 473 """ 464 474 Takes a list of dicts, returns that list sorted by the property given in 465 475 the argument. 466 476 """ 467 var_resolve = Variable(arg).resolve 468 decorated = [(var_resolve(item), item) for item in value] 469 decorated.sort() 470 return [item[1] for item in decorated] 477 return _dictsort(value, arg, reverse=False) 471 478 dictsort.is_safe = False 472 479 473 480 def dictsortreversed(value, arg): … … 475 482 Takes a list of dicts, returns that list sorted in reverse order by the 476 483 property given in the argument. 477 484 """ 478 var_resolve = Variable(arg).resolve 479 decorated = [(var_resolve(item), item) for item in value] 480 decorated.sort() 481 decorated.reverse() 482 return [item[1] for item in decorated] 485 return _dictsort(value, arg, reverse=True) 483 486 dictsortreversed.is_safe = False 484 487 485 488 def first(value):