Error: object of type 'int' has no len()
<p>I have a function that is supposed to take an array and sort it from the smallest string size to the largest and then print it by using injection sort.</p>

<p>On the line: <code>while (j &gt;=0) and (len(b[j]) &gt; key):</code></p>

<p>I get this error: </p>

<blockquote>
  <p>object of type 'int' has no len()</p>
</blockquote>

<p>Code:</p>

<pre><code>def list_sort(b):
    for i in range(1, len(b)):
        key = len(b[i])
        j = i - 1 
        while (j &gt;=0) and (len(b[j]) &gt; key):
            b[j+1] = b[j]
            j = j - 1
        b[j+1] = key
    for i in range( len(b) ):
        print (b[i])

list_sort(name)
</code></pre>

<p>I am not sure how you generated the error to begin with, since the input of <code>["alice", "bob", "sally"]</code> didn't crash, but it does generate numbers. </p>

<p>That's probably because you assign <code>b_list[j+1] = keyValue</code>, where <code>keyValue = len(b_list[i])</code>, which will be an int, and so, if you ever hit that element again, and try to <code>len()</code> on it, that's the error you are seeing. </p>

<p>Now, re-implementing this from the <a href="https://en.wikipedia.org/wiki/Insertion_sort#Algorithm_for_insertion_sort" rel="nofollow">Wikipedia pseudo-code</a></p>

<pre><code>def insertion_sort(A):
    for i in range(1, len(A)):
        j = i
        s1 = A[j-1]
        s2 = A[j]
        while j &gt; 0 and len(s1) &gt; len(s2):
            # swap A[j] and A[j-1]
            tmp = A[j-1]
            A[j-1] = A[j]
            A[j] = tmp

            j = j - 1

name_list = ["alice", "bob", "sally"]
insertion_sort(name_list)

for name in name_list:
    print name
</code></pre>

<p>Prints out </p>

<pre><code>bob
alice
sally
</code></pre>

python arrays sorting