title: Why is this Python function to sort integers contained as strings slower than this?
body: <p>I have two python functions for sorting a list of integers contained as strings. They are follows:</p>
  <pre><code>import random
  
  n = 10000000
  unsorted = [str(x) for x in range(n)]
  random.shuffle(unsorted)
  
  def sort_alg1(unsorted):
      unsorted.sort(key = lambda x: int(x))
      return unsorted
  
  def sort_alg2(unsorted):
      l=list(map(int,unsorted))
      l.sort()
      s=list(map(str,l))
      return s
  
  print(sort_alg1(unsorted))
  print(sort_alg2(unsorted))
  </code></pre>
  <p>Both work as expected. However, according to my profiler (I am using the ever-popular <a href="https://github.com/rkern/line_profiler" rel="nofollow noreferrer">line_profiler</a> by Robert Kern), the first function i.e. <code>sort_alg1</code> is more than 3 times slower in execution as <code>sort_alg2</code>. Now that wouldn't be a big problem if I could pinpoint the reason for this, but I am unable to. I have tried looking up differences in the built-in <code>sort()</code> method and <code>map()</code> function, lambda, etc. all to no avail. If someone could educate me as to why this is happening, that would be really great!</p>