Trying to sort a list by values closest to zero, using absolute value in python
<p>I've spent quite some time searching to see if there was a similar issue. Unfortunately, I couldn't just comment on the previously asked question to see why my attempt was not working. 
On this site there exists a question where a person wants to sort a list, according to closest numbers to a specific value.
    his example:<em>not verbatim copying his example but putting a working solution</em></p>

<pre><code>list_x = [1,2,5,11]
list_x.sort(key = lambda x: abs(x-4))
</code></pre>

<p>which will return the list sorted by values closest to 4 <code>(5,2,1,11)</code>
However, when trying to sort a much larger list by values closest to zero this is happening to me:</p>

<pre><code>listA.sort(key = lambda x: abs(x))

Traceback (most recent call last):
  File "&lt;pyshell#382&gt;", line 1, in &lt;module&gt;
    listA.sort(key = lambda x: abs(x))
  File "&lt;pyshell#382&gt;", line 1, in &lt;lambda&gt;
    listA.sort(key = lambda x: abs(x))
TypeError: bad operand type for abs(): 'str'
</code></pre>

<p>What exactly am I doing incorrectly thats preventing the list from being sorted by absolute values?</p>

<p>This is input data for what makes listA</p>

<pre><code>for i in decrange(0, 13, i):
   code = (func(seq, float(i)))
   codes = '%.3f' % code
   listA.append(float(codes))
   listB.append(str(codes))
listA.sort(key = lambda k: abs(k))
x = listA[0]
answer = listB.index(x) * float(.01)
</code></pre>
