title: How to pass additional arguments to custom python sorting function
body: <h2>Background:</h2>
  
  <p>I would like to know how I can implement advanced sorting functions that I can pass in as tuple element to the key argument of the python 'sorted' function.</p>
  
  <p>Here is an example depicting what I would like to do:</p>
  
  <pre><code>class Book:
  
        def __init__(self, name, author, language, cost):
            self.name = name
            self.author = author
            self.language=language
            self.cost = cost
  
  
  bookList = [list of books]
  
  firstLanguage = "Armenian"
  possibleLanguages = ["English", "Spanish", "Armenian", "French", "Chinese", "Swahili"]
  possibleLanguages.remove("Armenian")
  
  sortedBookList = sorted(bookList, key=(sortByName,
      sortByFirstLanguage(firstLanguage), sortByLanguages(possibleLanguages) ))
  </code></pre>
  
  <p>Basically I would like to implement the 'sortByFirstLanguage' and 'sortByLanguages' functions described above so that I can pass them to the python 'sorted' function as the tuple items of the 'key' argument.  Here is some example code regarding what the custom sort functions should look like:</p>
  
  <pre><code>def sortByName(elem):
      return elem.name
  
  def sortByFirstLanguage(elem, firstLanguage):
      if elem.language == firstLanguage:
         return 1
      else:
         return -1
  
  
  def sortByLanguages(elem, possibleLanguages):
      if elem.language in possibleLanguages:
         return possibleLanguages.index(elem.language)
  </code></pre>
  
  <h2>Addt. Details:</h2>
  
  <ol>
  <li>I am using python 2.7 </li>
  <li>This problem is actually using Django querysets rather than lists of objects, but for demonstration of purpose, I think a list of objects serves the same purpose.</li>
  <li>The goal of this sorting is to sort by a specified language first, then go back &amp;&amp; sort the remaining items by their default ordering (list ordering in this case).</li>
  </ol>
  
  <h2>Question:</h2>
  
  <p>How exactly can I tell the 'key' argument to pass in the extra arguments 'firstLanguage' &amp;&amp; 'possibleLanguages' to custom sorting functions as I have shown above?</p>