Schwartzian sort example in "Text Processing in Python"
<p>I was browsing through "Text Processing in Python" and tried its <a href="http://gnosis.cx/TPiP/038.code" rel="nofollow">example</a> about Schwartzian sort. </p>

<p>I used following structure for sample data which also contains empty lines. I sorted this data by fifth column:<br />
383230 -49 -78 1 <strong>100034</strong> '06 text' 9562 'text' 720 'text' 867<br />
335067 -152 -18 3 <strong>100030</strong> 'text' 2400 'text' 2342 'text' 696<br />
136592 21 230 3 <strong>100035</strong> '03. text' 10368 'text' 1838 'text' 977  </p>

<p>Code used for Schwartzian sorting:</p>

<pre><code>for n in range(len(lines)):       # Create the transform
    lst = string.split(lines[n])
    if len(lst) &gt;= 4:             # Tuple w/ sort info first
        lines[n] = (lst[4], lines[n])
    else:                         # Short lines to end
        lines[n] = (['\377'], lines[n])

lines.sort()    # Native sort

for n in range(len(lines)):       # Restore original lines
    lines[n] = lines[n][1]

open('tmp.schwartzian','w').writelines(lines)
</code></pre>

<p>I don't get how the author intended that short or empty lines should go to end of file by using this code. Lines are sorted after the if-else structure, thus raising empty lines to top of file. Short lines of course work as supposed with the custom sort (fourth_word function) as implemented in the example. </p>

<p>This is now bugging me, so any ideas? If I'm correct about this then how would you ensure that short lines actually stay at end of file?</p>

<p><strong>EDIT:</strong> I noticed the square brackets around '\377'. This messed up sort() so I removed those  brackets and output started working. </p>

<pre><code>else:                         # Short lines to end
    lines[n] = (['\377'], lines[n])
print type(lines[n][0])
&gt;&gt;&gt; (type 'list')
</code></pre>

<p>I accepted nosklo's answer for good clarification about the meaning of '\377' and for his improved algorithm. Many thanks for the other answers also!</p>

<p>If curious, I used 2 MB sample file which took 0.95 secs with the custom sort and 0.09 with the Schwartzian sort while creating identical output files. It works!</p>
