Browser Sorting.

 So here is a quick note on something I found out. When using Array sort in JS, you can have varying results depending on whether you are using Chrome or Firefox. It's weird to think, but Firefox implements stable sorting, whereas the Chrome browser does not. A stable sorting algorithm will place items found to be equal, i.e. zero, in the position they were iterated. Unstable sorting does not and can cause an ordered list to become unordered. I know in my case I was trying to sort items by date and on Firefox everything was OK, but Chrome had one item way out of place at the top and the rest in reverse order. I noticed the error on my mobile device and originally thought it to be unique to android, but once I loaded Chrome on my PC, I knew it was more than that. 

So the solution is very simple. Rather than leave the result up to the browser to decide. You hard code the options, thus remove any chance for errors. 

sort (a, b) {

return a - b; //here has three possible answers

}

sort (a, b) {

return a - b ? 1 : -1; //here has only two

}



Post a Comment

Previous Post Next Post