Dec-11-2017, 10:13 PM
(Dec-11-2017, 09:12 PM)ODIS Wrote: Python list object has built-in function for sorting. You can create function sort_people for your case like this way:
import collections Person = collections.namedtuple('Person', ['Name', 'Age', 'Height', 'Weight']) person1 = Person(Name='Bob', Age=55, Height=165, Weight=70) person2 = Person(Name='Doe', Age=45, Height=185, Weight=90) person3 = Person(Name='John', Age=50, Height=175, Weight=85) people = [person1, person2, person3] def sort_people(people, attribute, reverse=False): people.sort(key=lambda x: getattr(x, attribute), reverse=reverse) # order by age sort_people(people, "Age") print(str(people))If you'll want to reverse ordering, just put True as a third parameter into the function.
Good, your code works.
Thanks!