title: How do operator.itemgetter() and sort() work?
body: <p>I have the following code:</p>
  <pre><code># initialize
  a = []
  
  # create the table (name, age, job)
  a.append([&quot;Nick&quot;, 30, &quot;Doctor&quot;])
  a.append([&quot;John&quot;,  8, &quot;Student&quot;])
  a.append([&quot;Paul&quot;, 22, &quot;Car Dealer&quot;])
  a.append([&quot;Mark&quot;, 66, &quot;Retired&quot;])    
  
  # sort the table by age
  import operator
  a.sort(key=operator.itemgetter(1))    
  
  # print the table
  print(a)
  </code></pre>
  <p>It creates a 4x3 table and then it sorts it by age. My question is, what exactly <code>key=operator.itemgetter(1)</code> does? Does the <code>operator.itemgetter</code> function return the item's value? Why can't I just type something like <code>key=a[x][1]</code> there? Or can I? How could with operator print a certain value of the form like <code>3x2</code> which is <code>22</code>?</p>
  <ol>
  <li><p>How does exactly Python sort the table? Can I reverse-sort it?</p>
  </li>
  <li><p>How can I sort it based on two columns like first age, and then if age is the same b name?</p>
  </li>
  <li><p>How could I do it without <code>operator</code>?</p>
  </li>
  </ol>