In Python, is there a function that classifies and orders an array of objects by an attribute?
Example:
class Book:
    """A Book class"""
    def __init__(self,name,author,year):
        self.name = name
        self.author = author
        self.year = year
hp1 = Book("Harry Potter and the Philosopher's stone","J.k Rowling",1997)
hp2 = Book("Harry Potter and the chamber of secretse","J.k Rowling",1998)
hp3 = Book("Harry Potter and the Prisioner of Azkaban","J.k Rowling",1999)
#asoiaf stands for A Song of Ice and Fire
asoiaf1 = Book("A Game of Thrones","George R.R Martin",1996)
asoiaf2 = Book("A Clash of Kings","George R.R Martin",1998)
hg1 = Book("The Hunger Games","Suzanne Collins",2008)
hg2 = Book("Catching Fire","Suzanne Collins",2009)
hg3 = Book("Mockingjaye","Suzanne Collins",2010);
books = [hp3,asoiaf1,hp1,hg1,hg2,hp2,asoiaf2,hg3]
#disordered on purpose
organized_by_autor = magic_organize_function(books,"author")
Does the magic_organize_function exist? Otherwise, what would it be?
sorted()function in Python takes akeyparameter. It is used to specify how you want your list sorted. In your case you want to sort by theauthorattribute of each element in the listbooks:organized_by_autor = sorted(books, lambda book: book.author);are superfluous. They don't do anything.