<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Array.sort browser differences</title>
	<atom:link href="http://www.allenpike.com/2009/arraysort-browser-differences/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.allenpike.com/2009/arraysort-browser-differences/</link>
	<description>Allen Pike on gidgets and gazmos.</description>
	<lastBuildDate>Tue, 07 Feb 2012 01:30:54 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
	<item>
		<title>By: James</title>
		<link>http://www.allenpike.com/2009/arraysort-browser-differences/comment-page-1/#comment-4169</link>
		<dc:creator>James</dc:creator>
		<pubDate>Mon, 26 Apr 2010 09:45:29 +0000</pubDate>
		<guid isPermaLink="false">http://www.antipode.ca/?p=762#comment-4169</guid>
		<description>This might be of use, especially the code snippets at the bottom -- works fine in Chrome!
http://code.google.com/p/v8/issues/detail?id=103#makechanges</description>
		<content:encoded><![CDATA[<p>This might be of use, especially the code snippets at the bottom &#8212; works fine in Chrome!<br />
<a href="http://code.google.com/p/v8/issues/detail?id=103#makechanges" rel="nofollow">http://code.google.com/p/v8/issues/detail?id=103#makechanges</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Alain Saint-Etienne</title>
		<link>http://www.allenpike.com/2009/arraysort-browser-differences/comment-page-1/#comment-4142</link>
		<dc:creator>Alain Saint-Etienne</dc:creator>
		<pubDate>Tue, 16 Mar 2010 14:53:02 +0000</pubDate>
		<guid isPermaLink="false">http://www.antipode.ca/?p=762#comment-4142</guid>
		<description>David Flanagan&#039;s &quot;JavaScript, The Definitive Guide&quot; (O&#039;Reilly, 3rd Edition, 1998(!)) states that the custom sort function should return 0 (zero) if a and b are equivalent, &quot;a number less than zero&quot; if a should appear before b, or &quot;a number greater than zero&quot; if a should appear after b.

It seems to me the issue is not different implementations across browser, as they all seem to behave as expected if a number is returned.

I&#039;m afraid we developpers just got accustomed to coding by the browser implementations (and their specific shortcuts, like accepting that function returning a boolean), rather than coding by the book.

It&#039;s a nice outcome that renewed browser competition pushes us back to reading the specs, and coding in a little more... standard-compliant way.
We can&#039;t expect browser vendors to provide standard-compliant code, if they have to deal with non-standard web developer code, can we ?</description>
		<content:encoded><![CDATA[<p>David Flanagan&#8217;s &#8220;JavaScript, The Definitive Guide&#8221; (O&#8217;Reilly, 3rd Edition, 1998(!)) states that the custom sort function should return 0 (zero) if a and b are equivalent, &#8220;a number less than zero&#8221; if a should appear before b, or &#8220;a number greater than zero&#8221; if a should appear after b.</p>
<p>It seems to me the issue is not different implementations across browser, as they all seem to behave as expected if a number is returned.</p>
<p>I&#8217;m afraid we developpers just got accustomed to coding by the browser implementations (and their specific shortcuts, like accepting that function returning a boolean), rather than coding by the book.</p>
<p>It&#8217;s a nice outcome that renewed browser competition pushes us back to reading the specs, and coding in a little more&#8230; standard-compliant way.<br />
We can&#8217;t expect browser vendors to provide standard-compliant code, if they have to deal with non-standard web developer code, can we ?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Greg Burghardt</title>
		<link>http://www.allenpike.com/2009/arraysort-browser-differences/comment-page-1/#comment-4114</link>
		<dc:creator>Greg Burghardt</dc:creator>
		<pubDate>Mon, 01 Feb 2010 14:26:55 +0000</pubDate>
		<guid isPermaLink="false">http://www.antipode.ca/?p=762#comment-4114</guid>
		<description>The only thing that I see as an issue with using subtraction is how you subtract non numbers? When sorting strings, the minus sign will cause the browser to convert the strings into numbers, and if you are sorting non numeric strings, you will be performing subtraction with NaN (Not a Number) values. Subtracting &quot;a&quot; from &quot;b&quot; probably works in your code because you are sorting numbers. Things get a little trickier when you have to sort booleans, strings, and dates. For this reason I highly recommend explicitly testing for less-than, equality, and greater-than, and then returning -1, 0 or +1.

I&#039;m not in any way trying to argue. I just got done spending a week banging my head against the wall with client side sorting, and thought I&#039;d save you a few dents and bruises. :)</description>
		<content:encoded><![CDATA[<p>The only thing that I see as an issue with using subtraction is how you subtract non numbers? When sorting strings, the minus sign will cause the browser to convert the strings into numbers, and if you are sorting non numeric strings, you will be performing subtraction with NaN (Not a Number) values. Subtracting &#8220;a&#8221; from &#8220;b&#8221; probably works in your code because you are sorting numbers. Things get a little trickier when you have to sort booleans, strings, and dates. For this reason I highly recommend explicitly testing for less-than, equality, and greater-than, and then returning -1, 0 or +1.</p>
<p>I&#8217;m not in any way trying to argue. I just got done spending a week banging my head against the wall with client side sorting, and thought I&#8217;d save you a few dents and bruises. :)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Allen</title>
		<link>http://www.allenpike.com/2009/arraysort-browser-differences/comment-page-1/#comment-4113</link>
		<dc:creator>Allen</dc:creator>
		<pubDate>Fri, 29 Jan 2010 18:36:19 +0000</pubDate>
		<guid isPermaLink="false">http://www.antipode.ca/?p=762#comment-4113</guid>
		<description>Interesting insight Greg. Although my solution of returning (a - b) doesn&#039;t return strictly {-1, 0, 1}, it does work in all browsers I tried since they seem to be testing for &lt;0, 0, and &gt;0.</description>
		<content:encoded><![CDATA[<p>Interesting insight Greg. Although my solution of returning (a &#8211; b) doesn&#8217;t return strictly {-1, 0, 1}, it does work in all browsers I tried since they seem to be testing for &lt;0, 0, and >0.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Greg Burghardt</title>
		<link>http://www.allenpike.com/2009/arraysort-browser-differences/comment-page-1/#comment-4112</link>
		<dc:creator>Greg Burghardt</dc:creator>
		<pubDate>Fri, 29 Jan 2010 14:46:39 +0000</pubDate>
		<guid isPermaLink="false">http://www.antipode.ca/?p=762#comment-4112</guid>
		<description>As an aside, I have a theory why it seems to work on Firefox and no other browser. It seems that Firefox only calls the function you pass to Array.sort when a and b are NOT equal, and therefore only requires a test for greater-than or less-than. By how all other browsers behave, they seem to call the function you pass to Array.sort EVERY time, and leave it up to the function to determine equivalency, greater-than or less-than.</description>
		<content:encoded><![CDATA[<p>As an aside, I have a theory why it seems to work on Firefox and no other browser. It seems that Firefox only calls the function you pass to Array.sort when a and b are NOT equal, and therefore only requires a test for greater-than or less-than. By how all other browsers behave, they seem to call the function you pass to Array.sort EVERY time, and leave it up to the function to determine equivalency, greater-than or less-than.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Greg Burghardt</title>
		<link>http://www.allenpike.com/2009/arraysort-browser-differences/comment-page-1/#comment-4111</link>
		<dc:creator>Greg Burghardt</dc:creator>
		<pubDate>Fri, 29 Jan 2010 14:42:34 +0000</pubDate>
		<guid isPermaLink="false">http://www.antipode.ca/?p=762#comment-4111</guid>
		<description>The custom function passed to the Array.prototype.sort method has been wrong on almost every website I&#039;ve come across. This custom function should determine if a is greater than b, a is less than b, OR a and b are equal.

http://msdn.microsoft.com/en-us/library/4b4fbfhk%28VS.85%29.aspx

Only then will the custom sort function work correctly cross-browser. It was Microsoft&#039;s documentation that shed some light on this. It works cross browser in my testing on Firefox (Gecko), IE (Trident), Safari (Webkit/Apple) and Chrome (Webkit/Google).

myArray.sort( function( a, b ) {

if ( a === b ) return 0; // a and b are equal

if ( a &lt; b ) return -1; // a is less than b, b comes first

else return 1; // a is greater than b, a comes first

} );

The number you return for &quot;a is greater than or less than b&quot; will depend on whether you are sorting by ascending or descending order.

The function passed to Array.sort should return:

0 (zero) if a and be are EQUAL.

1 if a is GREATER than b

-1 if a is LESS than b

Returning a boolean value just tells the browser less-than or greater-than, and omits equal-to. Since many browsers are written in C, the number -1 is logically equivalent to true. not only is returning a boolean value omitting a possible value for the sort (a and b are equal), it could be interpreted incorrectly depending on the programming language the browser is written in.

Always return 0, 1 or -1 from Array.sort. Never return a boolean value.</description>
		<content:encoded><![CDATA[<p>The custom function passed to the Array.prototype.sort method has been wrong on almost every website I&#8217;ve come across. This custom function should determine if a is greater than b, a is less than b, OR a and b are equal.</p>
<p><a href="http://msdn.microsoft.com/en-us/library/4b4fbfhk%28VS.85%29.aspx" rel="nofollow">http://msdn.microsoft.com/en-us/library/4b4fbfhk%28VS.85%29.aspx</a></p>
<p>Only then will the custom sort function work correctly cross-browser. It was Microsoft&#8217;s documentation that shed some light on this. It works cross browser in my testing on Firefox (Gecko), IE (Trident), Safari (Webkit/Apple) and Chrome (Webkit/Google).</p>
<p>myArray.sort( function( a, b ) {</p>
<p>if ( a === b ) return 0; // a and b are equal</p>
<p>if ( a &lt; b ) return -1; // a is less than b, b comes first</p>
<p>else return 1; // a is greater than b, a comes first</p>
<p>} );</p>
<p>The number you return for &quot;a is greater than or less than b&quot; will depend on whether you are sorting by ascending or descending order.</p>
<p>The function passed to Array.sort should return:</p>
<p>0 (zero) if a and be are EQUAL.</p>
<p>1 if a is GREATER than b</p>
<p>-1 if a is LESS than b</p>
<p>Returning a boolean value just tells the browser less-than or greater-than, and omits equal-to. Since many browsers are written in C, the number -1 is logically equivalent to true. not only is returning a boolean value omitting a possible value for the sort (a and b are equal), it could be interpreted incorrectly depending on the programming language the browser is written in.</p>
<p>Always return 0, 1 or -1 from Array.sort. Never return a boolean value.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Allen</title>
		<link>http://www.allenpike.com/2009/arraysort-browser-differences/comment-page-1/#comment-3874</link>
		<dc:creator>Allen</dc:creator>
		<pubDate>Sun, 26 Jul 2009 18:01:22 +0000</pubDate>
		<guid isPermaLink="false">http://www.antipode.ca/?p=762#comment-3874</guid>
		<description>That&#039;s frustrating. Yet more reasons to use a framework that abstracts this out, I guess.</description>
		<content:encoded><![CDATA[<p>That&#8217;s frustrating. Yet more reasons to use a framework that abstracts this out, I guess.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Boris</title>
		<link>http://www.allenpike.com/2009/arraysort-browser-differences/comment-page-1/#comment-3873</link>
		<dc:creator>Boris</dc:creator>
		<pubDate>Sat, 25 Jul 2009 22:54:56 +0000</pubDate>
		<guid isPermaLink="false">http://www.antipode.ca/?p=762#comment-3873</guid>
		<description>The sorting stability of Array.sort also depends on the browser&#039;s implementation. IE&#039;s Array.sort is stable but Firefox pre-3.5 makes no such guarantees. Even more annoyingly, I think Safari&#039;s Array.sort is stable, but Chrome&#039;s isn&#039;t (don&#039;t quote me on this one though).

I&#039;ve hit a few obscure problems because of this when trying to double sort objects.</description>
		<content:encoded><![CDATA[<p>The sorting stability of Array.sort also depends on the browser&#8217;s implementation. IE&#8217;s Array.sort is stable but Firefox pre-3.5 makes no such guarantees. Even more annoyingly, I think Safari&#8217;s Array.sort is stable, but Chrome&#8217;s isn&#8217;t (don&#8217;t quote me on this one though).</p>
<p>I&#8217;ve hit a few obscure problems because of this when trying to double sort objects.</p>
]]></content:encoded>
	</item>
</channel>
</rss>

