Created
March 31, 2018 14:39
-
-
Save Fhernd/3cddd3bfe3493bca5462450141fa46c7 to your computer and use it in GitHub Desktop.
Ordenar objetos sin comparación nativa. OrtizOL.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from operator import attrgetter | |
class Usuario: | |
def __init__(self, id): | |
self.id = id | |
def __repr__(self): | |
return 'Usuario({})'.format(self.id) | |
usuarios = [Usuario(13), Usuario(2), Usuario(11), Usuario(7)] | |
print(usuarios) | |
print(sorted(usuarios, key=lambda u: u.id)) | |
print('') | |
print(sorted(usuarios, key=attrgetter('id'))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment