Skip to content

Instantly share code, notes, and snippets.

@Fhernd
Created March 31, 2018 14:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Fhernd/3cddd3bfe3493bca5462450141fa46c7 to your computer and use it in GitHub Desktop.
Save Fhernd/3cddd3bfe3493bca5462450141fa46c7 to your computer and use it in GitHub Desktop.
Ordenar objetos sin comparación nativa. OrtizOL.
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