I agree that that function will fix the problem. You can also use it to fix sorting any mixed types but that's not my point either. My point is that when you have None mixed in with numbers, sorting doesn'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] < sorted[j] for all i < j and it doesn't. I realize that it *can't* satisfy it for the nans because < is always false regardless. But it should at least satisfy it for the comparable values and it doesn'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"><<a href="mailto:jyasskin@gmail.com">jyasskin@gmail.com</a>></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'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>
if x is None:<br>
return (1, x)<br>
if isnan(x):<br>
return (3, x)<br>
return (2, x)<br>
<br>
No need to modify either sort() or <.<br>
<div><div></div><div class="Wj3C7c"><br>
On Thu, Nov 13, 2008 at 11:15 AM, Bruce Leban <<a href="mailto:bruce@leapyear.org">bruce@leapyear.org</a>> wrote:<br>
> I think the behavior of NaN in comparisons is more confusing:<br>
>>>> sorted([1,nan,2])<br>
> [1, nan, 2]<br>
>>>> sorted([2,nan,1])<br>
> [2, nan, 1]<br>
>>>> sorted([2,None,1])<br>
> Traceback (most recent call last):<br>
> File "<pyshell#28>", line 1, in <module><br>
> sorted([2,None,1])<br>
> TypeError: unorderable types: NoneType() < int()<br>
> At least the third case is clear that I shouldn't have done that. The way<br>
> nan works, the results of sorting where one of the values is nan is<br>
> unpredictable and useless.<br>
> Yes, I know the rules about how NaN values behave in comparisons.<br>
> Notwithstanding that, sorting could use a different comparison rule imposing<br>
> a total ordering: -inf, ..., inf, nan as some other systems do.<br>
> --- Bruce<br>
><br>
> On Thu, Nov 13, 2008 at 10:55 AM, Tim Peters <<a href="mailto:tim.peters@gmail.com">tim.peters@gmail.com</a>> wrote:<br>
>><br>
>> [M.-A. Lemburg]<br>
>> > ...<br>
>> > So far, I haven't heard a single argument for why not having None<br>
>> > participate in an ordering scheme is a good strategy to use, except<br>
>> > that it's pure.<br>
>><br>
>> I've tracked down plenty of program logic errors that would have been<br>
>> discovered more easily if comparing None to (mostly, but among others)<br>
>> integers and strings had raised an exception instead of returning a<br>
>> meaningless true/false result. Perhaps you haven't. For those who<br>
>> have, the attraction to making comparisons with None refuse to return<br>
>> nonsense silently is both obvious and visceral.<br>
>><br>
>><br>
>> > IMHO, practicality beats purity in this special case.<br>
>><br>
>> If hiding program logic errors is practical, sure ;-)<br>
>><br>
>> there-is-no-behavior-no-matter-how-bizarre-someone-won't<br>
>> come-to-rely-on-ly y'rs - tim<br>
>> _______________________________________________<br>
>> Python-3000 mailing list<br>
>> <a href="mailto:Python-3000@python.org">Python-3000@python.org</a><br>
>> <a href="http://mail.python.org/mailman/listinfo/python-3000" target="_blank">http://mail.python.org/mailman/listinfo/python-3000</a><br>
>> Unsubscribe:<br>
>> <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>
><br>
><br>
> _______________________________________________<br>
> Python-3000 mailing list<br>
> <a href="mailto:Python-3000@python.org">Python-3000@python.org</a><br>
> <a href="http://mail.python.org/mailman/listinfo/python-3000" target="_blank">http://mail.python.org/mailman/listinfo/python-3000</a><br>
> Unsubscribe:<br>
</div></div>> <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>
><br>
><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>