for in li sort_num false sort( [ : : : : the first time we call sortit sorts by numbers because sort_num is true on all the objects being compared the second timeit sorts by letters the __lt__ method is the only one we need to implement to enable sorting technicallyhoweverif it is implementedthe class should normally also implement the similar __gt____ eq____ne____ge__and __le__ methods so that all of the ==!=>=and <operators also work properly you can get this for free by implementing __lt__ and __eq__and then applying the @total_ordering class decorator to supply the restfrom functools import total_ordering @total_ordering class 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 numberdef __eq__(selfobject)return all(self string =object stringself number =object numberself sort_num =object number )