title: python .sort() has higher priority for __lt__ than __gt__?
body: <p>I have seen similar questions, but they did not answer why python <code>__lt__</code> has higher priority than <code>__gt__</code>?</p>
  
  <p>Quick example, let me give a superclass and a subclass:</p>
  
  <pre><code>class Person(object):
      id = 0
      def __init__(self, name):
          self.name = name
          self.id = Person.id
          Person.id += 1
  
      def __str__(self):
          return self.name
  
      def __lt__(self, other):
          return self.id &lt; other.id
  
  class SubPerson(Person):
      def __gt__(self, other):
          return self.name &gt; other.name
  </code></pre>
  
  <p>Here in superclass <code>Person</code>, I created a <code>__lt__</code> method, to compare based on Person's <code>self.id</code>. In sub class <code>SubPerson</code>, I created a <code>__gt__</code> method to compare based on the <code>self.name</code>.</p>
  
  <p>What I found is that if I created a number of <code>SubPerson</code> instances in a list:</p>
  
  <pre><code>s1 = SubPerson('WWW'); s1.id = 14
  s2 = SubPerson('XXX'); s2.id = 12
  s3 = SubPerson('YYY'); s3.id = 11
  s4 = SubPerson('ZZZ'); s4.id = 13
  lst2 = [s2, s1, s4, s3]
  lst2.sort()
  print([i.__str__() for i in lst2])  # &gt;&gt;&gt; ['YYY', 'XXX', 'ZZZ', 'WWW']
  </code></pre>
  
  <p>What I found is that:
  if <code>__lt__</code> is defined in superclass, then even if you define <code>__gt__</code> in subclass, it will still sort by the <code>__lt__</code> method in superclass</p>
  
  <p>But if it is the other way around, define <code>__gt__</code> in superclass and <code>__lt__</code> in subclass, then it will then sort by <code>__lt__</code> in subclass</p>
  
  <p>If the two method names are the same (both <code>lt</code> or both <code>gt</code>), obviously, the subclass will override superclass, as it should be. </p>
  
  <p>But it seems when they are different, it follows a different rule: by whatever <code>__lt__</code> defines. I've also noticed that even if in one class, if both <code>__lt__</code> and <code>__gt__</code> are defined (based on different things, it will still sort by <code>__lt__</code> method)</p>
  
  <p>SO back to my question, is my observation true or not? and since I am a beginner, and I have not read through the whole python documents, can somebody point me out where in the documents that this rule is written.</p>