title: Python: How to custom order a list?
body: <p>Obs: I know lists in python are not order-fixed, but think that this one will be.
  And I'm using Python 2.4</p>
  
  <p>I have a list, like (for example) this one:</p>
  
  <pre><code>mylist = [ ( u'Article', {"...some_data..."}    ) ,
             ( u'Report' , {"...some_data..."}    ) ,
             ( u'Book'   , {"...another_data..."} ) ,
  ...#continue
  ]
  </code></pre>
  
  <p>This variable mylist is obtained from a function, and the 'order' of the list returned will vary. So, sometimes it will be like on the example. Sometimes, the 'Report' will come before 'Article', etc.</p>
  
  <p>I have a fixed order that I want on this list (and isn't the alphabetical).</p>
  
  <p>Let's say that my fixed order is: 'Report', 'Article', 'Book', ...</p>
  
  <p>So, what I want is that: whatever order 'mylist' is instantiated, I want to reorder it making 'Report' stay on front, 'Article' on second, etc...</p>
  
  <p>What's the best approach to reorder my list (taking the first element of the tuple of each item on list) using my 'custom' order?</p>
  
  <p><strong>Answer</strong>:</p>
  
  <p>I ended up with this:<br/></p>
  
  <p>mylist became a list of dicts, like this:</p>
  
  <pre><code>mylist = [{'id':'Article', "...some_data..."} ,
          ...etc
  ]
  </code></pre>
  
  <p>each dict having a 'id' that had to be sorted. </p>
  
  <p>Saving the correct order on a listAssigning the correct_order on a list:</p>
  
  <pre><code>correct_order = ['Report', 'Article', 'Book', ...]
  </code></pre>
  
  <p>and doing: </p>
  
  <pre><code>results = sorted([item for item in results], cmp=lambda x,y:cmp(correct_order.index(x['id']), correct_order.index(y['id'])))
  </code></pre>