Advertisement
Fhernd

ordenar-objetos.py

Mar 31st, 2018
2,072
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.34 KB | None | 0 0
  1. from operator import attrgetter
  2.  
  3. class Usuario:
  4.     def __init__(self, id):
  5.         self.id = id
  6.        
  7.     def __repr__(self):
  8.         return 'Usuario({})'.format(self.id)
  9.        
  10. usuarios = [Usuario(13), Usuario(2), Usuario(11), Usuario(7)]
  11.  
  12. print(usuarios)
  13.  
  14. print(sorted(usuarios, key=lambda u: u.id))
  15.  
  16. print('')
  17.  
  18. print(sorted(usuarios, key=attrgetter('id')))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement