if we want to place objects we define ourselves into list and make those objects sortablewe have to do bit more work the special method __lt__which stands for "less than"should be defined on the class to make instances of that class comparable the sort method on list will access this method on each object to determine where it goes in the list this method should return true if our class is somehow less than the passed parameterand false otherwise here' rather silly class that can be sorted based on either string or numberclass weirdsorteedef __init__(selfstringnumbersort_num)self string string self number number self sort_num sort_num def __lt__(selfobject)if self sort_numreturn self number object number return self string object string def __repr__(self)return"{}:{}format(self stringself numberthe __repr__ method makes it easy to see the two values when we print list the __lt__ method' implementation compares the object to another instance of the same class (or any duck typed object that has stringnumberand sort_num attributesit will fail if those attributes are missingthe following output illustrates this class in actionwhen it comes to sortinga weirdsortee(' ' trueb weirdsortee(' ' truec weirdsortee(' ' trued weirdsortee(' ' truel [ , , ,dl [ : : : : sort( [ : : : :