I agree that that function will fix the problem. You can also use it to fix sorting any mixed types but that&#39;s not my point either. My point is that when you have None mixed in with numbers, sorting doesn&#39;t work in a way that stops you and makes you fix it. When you have nan mixed in, it just fails randomly.<br>
<br>The sorted function should satisfy the property that sorted[i] &lt; sorted[j] for all i &lt; j and it doesn&#39;t. I realize that it *can&#39;t* satisfy it for the nans because &lt; is always false regardless. But it should at least satisfy it for the comparable values and it doesn&#39;t.<br>
<br>--- Bruce<br><br><br><div class="gmail_quote">On Thu, Nov 13, 2008 at 1:01 PM, Jeffrey Yasskin <span dir="ltr">&lt;<a href="mailto:jyasskin@gmail.com">jyasskin@gmail.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Be glad you&#39;re not programming in C++ then, where trying to sort NaN<br>
can cause segfaults!<br>
<br>
More seriously, I think using the following function as the sort key<br>
will make sort do what you want:<br>
<br>
def SortNoneFirstAndNanLast(x):<br>
 &nbsp;if x is None:<br>
 &nbsp; &nbsp;return (1, x)<br>
 &nbsp;if isnan(x):<br>
 &nbsp; &nbsp;return (3, x)<br>
 &nbsp;return (2, x)<br>
<br>
No need to modify either sort() or &lt;.<br>
<div><div></div><div class="Wj3C7c"><br>
On Thu, Nov 13, 2008 at 11:15 AM, Bruce Leban &lt;<a href="mailto:bruce@leapyear.org">bruce@leapyear.org</a>&gt; wrote:<br>
&gt; I think the behavior of NaN in comparisons is more confusing:<br>
&gt;&gt;&gt;&gt; sorted([1,nan,2])<br>
&gt; [1, nan, 2]<br>
&gt;&gt;&gt;&gt; sorted([2,nan,1])<br>
&gt; [2, nan, 1]<br>
&gt;&gt;&gt;&gt; sorted([2,None,1])<br>
&gt; Traceback (most recent call last):<br>
&gt; &nbsp; File &quot;&lt;pyshell#28&gt;&quot;, line 1, in &lt;module&gt;<br>
&gt; &nbsp; &nbsp; sorted([2,None,1])<br>
&gt; TypeError: unorderable types: NoneType() &lt; int()<br>
&gt; At least the third case is clear that I shouldn&#39;t have done that. The way<br>
&gt; nan works, the results of sorting where one of the values is nan is<br>
&gt; unpredictable and useless.<br>
&gt; Yes, I know the rules about how NaN values behave in comparisons.<br>
&gt; Notwithstanding that, sorting could use a different comparison rule imposing<br>
&gt; a total ordering: -inf, ..., inf, nan as some other systems do.<br>
&gt; --- Bruce<br>
&gt;<br>
&gt; On Thu, Nov 13, 2008 at 10:55 AM, Tim Peters &lt;<a href="mailto:tim.peters@gmail.com">tim.peters@gmail.com</a>&gt; wrote:<br>
&gt;&gt;<br>
&gt;&gt; [M.-A. Lemburg]<br>
&gt;&gt; &gt; ...<br>
&gt;&gt; &gt; So far, I haven&#39;t heard a single argument for why not having None<br>
&gt;&gt; &gt; participate in an ordering scheme is a good strategy to use, except<br>
&gt;&gt; &gt; that it&#39;s pure.<br>
&gt;&gt;<br>
&gt;&gt; I&#39;ve tracked down plenty of program logic errors that would have been<br>
&gt;&gt; discovered more easily if comparing None to (mostly, but among others)<br>
&gt;&gt; integers and strings had raised an exception instead of returning a<br>
&gt;&gt; meaningless true/false result. &nbsp;Perhaps you haven&#39;t. &nbsp;For those who<br>
&gt;&gt; have, the attraction to making comparisons with None refuse to return<br>
&gt;&gt; nonsense silently is both obvious and visceral.<br>
&gt;&gt;<br>
&gt;&gt;<br>
&gt;&gt; &gt; IMHO, practicality beats purity in this special case.<br>
&gt;&gt;<br>
&gt;&gt; If hiding program logic errors is practical, sure ;-)<br>
&gt;&gt;<br>
&gt;&gt; there-is-no-behavior-no-matter-how-bizarre-someone-won&#39;t<br>
&gt;&gt; come-to-rely-on-ly y&#39;rs &nbsp;- tim<br>
&gt;&gt; _______________________________________________<br>
&gt;&gt; Python-3000 mailing list<br>
&gt;&gt; <a href="mailto:Python-3000@python.org">Python-3000@python.org</a><br>
&gt;&gt; <a href="http://mail.python.org/mailman/listinfo/python-3000" target="_blank">http://mail.python.org/mailman/listinfo/python-3000</a><br>
&gt;&gt; Unsubscribe:<br>
&gt;&gt; <a href="http://mail.python.org/mailman/options/python-3000/bruce%40leapyear.org" target="_blank">http://mail.python.org/mailman/options/python-3000/bruce%40leapyear.org</a><br>
&gt;<br>
&gt;<br>
&gt; _______________________________________________<br>
&gt; Python-3000 mailing list<br>
&gt; <a href="mailto:Python-3000@python.org">Python-3000@python.org</a><br>
&gt; <a href="http://mail.python.org/mailman/listinfo/python-3000" target="_blank">http://mail.python.org/mailman/listinfo/python-3000</a><br>
&gt; Unsubscribe:<br>
</div></div>&gt; <a href="http://mail.python.org/mailman/options/python-3000/jyasskin%40gmail.com" target="_blank">http://mail.python.org/mailman/options/python-3000/jyasskin%40gmail.com</a><br>
&gt;<br>
&gt;<br>
<font color="#888888"><br>
<br>
<br>
--<br>
Namasté,<br>
Jeffrey Yasskin<br>
<a href="http://jeffrey.yasskin.info/" target="_blank">http://jeffrey.yasskin.info/</a><br>
</font></blockquote></div><br>