title: Using Insertion sort to sort list of dictionary
body: <p>This sort is For Educational purposes. no build in sort is allowed.</p>
  
  <p><strong>If you think my question and the answer helped, please vote me and the first answer people:
  @J.F. Sebastian</strong></p>
  
  <p>I have found this on SO --
  "<a href="https://stackoverflow.com/questions/15801027/how-to-do-an-insertion-sort-on-a-list-of-dictionaries-in-python">How to do an insertion sort on a list of dictionaries in python?</a>"</p>
  
  <p>but that answer does no seem right.</p>
  
  <p>Using the answer code of the above question raises this error:</p>
  
  <blockquote>
    <p>TypeError: list indices must be integers, not dict</p>
  </blockquote>
  
  <p>Example : </p>
  
  <pre><code>lst = [{'a':20, 'b':30, 'c':25, 'd': 600},{'a':60, 'b':10, 'c':43, 'd': 20}]`
  </code></pre>
  
  <p>to sort using insertion sort,  for example sort <code>b</code>, we should get</p>
  
  <pre><code>[{'a':60, 'b':10, 'c':43, 'd': 20},{'a':20, 'b':30, 'c':25, 'd': 600}]
  </code></pre>
  
  <p>But my code gets </p>
  
  <pre><code>[{'b': 10, 'c': 25, 'a': 20, 'd': 600}, {'b': 30, 'c': 43, 'a': 60, 'd': 20}]
  </code></pre>
  
  <p>he replace the key and value in the list of dictionary </p>
  
  <p>Here is my Code:    </p>
  
  <pre><code>def insertionSort(allData, key):
  
      for i in range(len(allData)):
          temp = allData[i][key]
          j = i
          while j &gt; 0 and temp &lt; allData[j - 1][key]:
                 allData[j][key] = allData[j - 1][key]
                 j = j - 1
          allData[j][key] = temp
  </code></pre>
  
  <p>My homework sort result:</p>
  
  <pre><code>{'snow': 19.2, 'minT': -10.8, 'month': 12, 'maxT': 9.0, 'rain': 45.0, 'year': 2003, 'meanT': -0.1, 'yearmonth': 193801}
  {'snow': 35.6, 'minT': -20.0, 'month': 1, 'maxT': 8.9, 'rain': 34.3, 'year': 1974, 'meanT': -5.9, 'yearmonth': 193802}
  {'snow': 0.0, 'minT': 9.7, 'month': 8, 'maxT': 34.8, 'rain': 20.8, 'year': 2007, 'meanT': 22.4, 'yearmonth': 193803}`
  </code></pre>
  
  <p>After sorting the <code>yearmonth</code>, they replace the <code>yearmonth</code> from small to big, but not changing the dictionary.</p>
  
  <p>Why does this happen, and how shoudl I change it?</p>
  
  <p>==================================</p>
  
  <p><strong>Answer:</strong></p>
  
  <p>After some basic copy of 'J.F. Sebastian' code
  I find that I can't directly using the code</p>
  
  <pre><code>sort(a,b)
  </code></pre>
  
  <p>output:</p>
  
  <blockquote>
    <p>TypeError: 'str' object is not callable</p>
  </blockquote>
  
  <p>I should use
      sort(a,b=lambda x: x['thekey'])
  then, JFS makes a new function to make it work.</p>
  
  <p>I also find another way:
  just change JFS code line 5:</p>
  
  <p><strong>from</strong></p>
  
  <pre><code>    if key(L[j]) &lt;= key(d):
  </code></pre>
  
  <p><strong><em>to</em></strong></p>
  
  <pre><code>    if L[j][key] &lt;= d[key]:
  </code></pre>
  
  <p>THEN EVERYTHING WORKS!
  Hope this can help other people too, and those using google and doing the same assignment as me.</p>