title: Overriding sorting attribute in class, python
body: <p>I would like to use bisect (as shown here, in the second answer: <a href="https://stackoverflow.com/questions/1109804/does-python-have-a-sorted-list">Does python have a sorted list?</a>),
  but instead of using a list of numbers, I have a list of objects. In specific, objects from this class: <a href="https://networkx.github.io/documentation/latest/_modules/networkx/classes/graph.html" rel="nofollow noreferrer">https://networkx.github.io/documentation/latest/_modules/networkx/classes/graph.html</a></p>
  
  <p>I would like the list to keep the graphs sorted by their number of nodes.
  If I push these graphs into the list, it looks like it is being inserted in an arbitrary way, (If I run it many times, it changes between the runs).</p>
  
  <p>Is there "sort" function that each class can define, that when applying sorting it will be used (like operator overriding in other languages) ?</p>
  
  <pre><code>import bisect
  import networkx as nx
  
  L=[]
  G1 = nx.Graph()
  G2 = nx.Graph()
  
  G1.add_edges_from([(1,2),(1,3),(2,3),(3,4),(4,5),(4,6),(5,6),(4,7),(7,8),(7,9),(8,9)])
  print 'G1', G1.number_of_nodes()
  G2.add_edges_from([(1,2),(1,3)])
  print 'G2', G2.number_of_nodes()
  
  bisect.insort(L,G1)
  bisect.insort(L,G2)
  
  print 'L0 ', L[0].number_of_nodes()
  print 'L1' ,L[1].number_of_nodes()
  </code></pre>
  
  <p>If there's another way of doing that, it would be great to.</p>
  
  <p>Thanks</p>