title: Java's TreeSet equivalent in Python?
body: <p>I recently came across some Java code that simply put some strings into a Java TreeSet, implemented a distance based comparator for it, and then made its merry way into the sunset to compute a given score to solve the given problem.</p>
  
  <p>My questions,</p>
  
  <ul>
  <li><p>Is there an equivalent data structure available for Python?</p>
  
  <ul>
  <li>The Java treeset looks basically to be an ordered dictionary that can use a comparator of some sort to achieve this ordering.</li>
  </ul></li>
  <li><p>I see there's a <a href="http://www.python.org/dev/peps/pep-0372/" rel="noreferrer">PEP for Py3K</a> for an OrderedDict, but I'm using 2.6.x. There are a bunch of ordered dict implementations out there - anyone in particular that can be recommended?</p></li>
  </ul>
  
  <p>PS, Just to add - I <em>could</em> probably import DictMixin or UserDict and implement my own sorted/ordered dictionary, AND make it happen through a comparator function - but that seems to be overkill.</p>
  
  <p>Thanks.</p>
  
  <hr>
  
  <p>Update. Thanks for the answers. To elaborate a bit, lets say I've got a compare function thats defined like, (given a particular value ln),</p>
  
  <pre><code>def mycmp(x1, y1, ln):
    a = abs(x1-ln)
    b = abs(y1-ln)
    if a&lt;b:
      return -1
    elif a&gt;b:
      return 1
    else:
      return 0
  </code></pre>
  
  <p>I'm a bit unsure about how I'd integrate this into the ordering given in the ordered dict <a href="http://docs.python.org/dev/library/collections.html#collections.OrderedDict" rel="noreferrer">link given here..</a>. </p>
  
  <p>Something like,</p>
  
  <pre><code>OrderedDict(sorted(d.items(), cmp=mycmp(len)))
  </code></pre>
  
  <p>Ideas would be welcome. </p>