Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python - Get sorted list of user-defined classes

I have a class:

class Prediction():
    def __init__(self, l):
        self.start = l[0]
        self.end = l[1]
        self.score = l[2]

And a list, where each element is a Prediction. It's aptly named predictions.

I want to sort predictions by the start attribute of the Prediction class.

Something like this:

predictions_start_order = sorted(predictions, key=start)

Which doesn't work. What am I missing?

like image 210
zzz Avatar asked Nov 26 '25 13:11

zzz


1 Answers

predictions_start_order = sorted(predictions, key=lambda x: x.start)
like image 64
log0 Avatar answered Nov 29 '25 04:11

log0



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!