Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sorting a list with objects of a class as its items

i have a python list whose items are objects of a class with various attributes such as birthday_score, anniversary_score , baby_score..... I want to to sort the list on the basis of one of these attributes ,say anniversary_score. How do i do it ?

like image 674
Chaitanya Agarwal Avatar asked Nov 30 '22 03:11

Chaitanya Agarwal


1 Answers

your_list.sort(key = lambda x : x.anniversary_score)

or if the attribute name is a string then you can use :

import operator
your_list.sort(key=operator.attrgetter('anniversary_score'))
like image 167
Ashwini Chaudhary Avatar answered Dec 06 '22 17:12

Ashwini Chaudhary