Task: Submit a file containing the sort_people(people) function. It gets a list of people and returns a sorted list of people. Sort primarily by date of birth (oldest person to youngest), if there is a match by last name (ascending according to the usual Python string comparison), and finally by first name (also ascending). If any two persons match in all three characteristics, their mutual order can be arbitrary.
I solved this task using key function for Python sort() method but it is not the best approach, since I only got 5/10 points because of time limitation.
Problem: Help me make this algorithm faster.
My code:
def compare_people(p1, p2):
date1 = datetime.strptime(p1.birth_date, '%d.%m.%Y')
date2 = datetime.strptime(p2.birth_date, '%d.%m.%Y')
if date1>date2:
return 1
elif date1<date2:
return -1
if p1.last_name > p2.last_name:
return 1
elif p1.last_name < p2.last_name:
return -1
if p1.first_name > p2.first_name:
return 1
elif p1.first_name < p2.first_name:
return -1
else:
return 0
def sort_people(people):
cmp1 = functools.cmp_to_key(compare_people)
people.sort(key=cmp1)
return people
CodePudding user response:
A tuple (and list) comparison compares its items in order, and returns the comparison of the first non-equal member. Thus, if you construct a key that provides a series of values to be compared in order, you can get the equivalent of your code like this:
people.sort(key=lambda p: (
datetime.strptime(p1.birth_date, '%d.%m.%Y'),
p.last_name,
p.first_name,
))
This has several advantages:
cmp_to_key
is invoked many times;key
is invoked exactly once per element- Comparison of tuples is implemented in C, so it will be much faster than your
compare_people
- It is brief and easy to read