Array.sort browser differences

July 25, 2009 • 1 min read


This post is over ten years old. Chances are, I've learned enough to have advanced my thinking about some of this stuff.

WebKit says, "Boolean comparison functions? What the farmer?"Javascript’s Array class has a handy function, sort(). You just pass in any comparison function, and it will sort the array for you. Therefore, the following code works:

[3,1,4,2,5].sort(function(a,b) {
    return a > b;
});

Or rather, it works in Firefox: it sorts the array to [1,2,3,4,5]. In IE, it seems to work at first glance: the 2 is out of place in the output. In Safari and Chrome, nothing changes at all. That’s because Webkit requires the comparison function to return an integer rather than a boolean, otherwise it does nothing with no error. The correct code is:

[3,1,4,2,5].sort(function(a,b) {
    return a - b;
});

This will work in all browsers. Something to keep in mind if you ever get a “Sorting doesn’t work in WebKit” bug and are scratching your head.


Liked this? Follow along to see what's next.

© Allen Pike. 👋🏼 You can contact me, or check out Steamclock.