August 26, 2008, 8:26 pm · 0 comments · Filed under: JavaScript
Can your web browser do this?
You’ll never get rich digging a ditch, nor building Dashboard widgets.
A Kryptonite™ lock can be defeated in 11 seconds, but you still lock your bike, right?
Gaining Twitter followers is a little like losing weight. You have to try.
Over or under? It’s the age-old question when it comes to the orientation of toilet paper rolls.
I am a web developer, recently returned to the States after 3 years in New Zealand. I’m into my family, photography and frisbee sports.
Nothing will benefit human health and increase chances for survival of life on earth as much as the evolution to a vegetarian diet.
–Albert Einstein
Apple · AppleScript · Business · Coda · CSS · Dashboard · Design · Google · InSTEDD · JavaScript · jQuery · Life · Marketing · Music · New Mexico · New Zealand · Open Source Software · Photography · PHP · Politics · Ruby on Rails · Scree · Subversion (SVN) · Twitter · Usability · Web Development · Widgets
CSS Fast Nav: Because (perception of) speed matters! · Personal Branding for Introverts · Stupid WebKit Tricks · Add an interactive legend to a MarkerManager managed Google Map · Dude. Mikeyy can’t even spell his own name. · Dashboard Widgets for Fun and Profit · Animating your iPhone web application · How-to recover from checksum mismatch errors in SVN · Why Apple can afford to charge so little for Snow Leopard · The first 48 hours of PHP Function Reference, by the numbers
CSS Fast Nav: Because (perception of) speed matters! · When is a global variable not a variable? · Our misguided culture of cool · InSTEDD: Open Source Software that saves lives · Add an interactive legend to a MarkerManager managed Google Map · Personal Branding for Introverts · Moments of Rangitoto · Some Twitter conventions · Why Apple can afford to charge so little for Snow Leopard · Stupid WebKit Tricks
Twitshirt is a tweet on a shirt. Buy the one below or check out my most recent tweets.
It's amazing how many more vegetables I eat now that I'm vegetarian.
See a random Twitshirt-worthy tweet.
80/20 · 90 Seven Design · Alyson Hurt · Andrew Nimick · Apps & Hats · Ben Young · Brian Arnold · Brian Warren · Carl Bolter · Chris Burgess · Christine Morris · Cristina Stoian · Daniel Lyons · Daniel Schwartz · David Hedges · Hamish Campbell · Jochen Daum · John Visser · Joseph McLaughlin · Joshua Sallach · Julian Pistorius · Justine Sanderson · Kalena Jordan · Katie Graham · Kelly Green · Kevin Potis · Mark Bixby · Matt Henry · Method Arts · Morgan Pyne · Peter Michaux · Philip Tellis · Piers Harding · Rebecca Murphey · Reid Givens · Rey Bango · Rhett Anderson · Richard Paul · Rob Pongsajapan · Robin Taylor · Ryan Park · Shaun Lee · Simon Young · Su Yin Khoo · Toni Barrett · Vaughan Rowsell · Vincent Thomé · Voom Studio
My bias is for references over “cookbooks.” I want to know all of my options, not just one way to do something. Show me the why as well as the how and I am happy.
JavaScript: The Good Parts · Object-Oriented JavaScript: Create scalable, reusable high-quality JavaScript applications and libraries · JavaScript: The Definitive Guide · Designing with Web Standards · CSS: The Definitive Guide · Prioritizing Web Usability · The Elements of User Experience · Web ReDesign: Workflow that Works · Don't Make Me Think: A Common Sense Approach to Web Usability
I’ve hosted this website with pair Networks since 1997. They rock.
This blog is powered by…software I wrote.
Feeling generous? Knock yourself out!
Now, maybe I shouldn’t be casting stones, considering my last blog post. The truth is, the technique those sites offered will work in most cases, but technically leaves scripts open to errors at the edges.
In JavaScript, you can sort an array just by calling its native sort method. By default, this sorts the contents as strings. That’s all fine and dandy when what you’re sorting are, in fact, strings, but not when you’re sorting numbers (e.g., 10 would come before 9).
The remedy for this is to pass a comparison function to the sort method. The comparison function needs to return -1 if the first argument should come before the second argument, 0 if they are equivalent in sort order, and 1 if the first argument should come after the second.
In most cases, the following syntax (suggested by both of the above linked sites) would work:
var myArray = [10, 9];
myArray.sort(function (a, b) {
return a - b;
});
// myArray is now [9, 10]
It’s terse. It’s simple. Looks good, right? Wrong.
The Mozilla Developer Center (an authoritative source, I reckon) has this to say about the above technique:
To compare numbers instead of strings, you should not subtract the numbers because this can cause overflow. For example,9e307-(-9e307)equalsNumber.POSITIVE_INFINITYbut so does9e307-(-9.9e307).
The solution is to be more explicit in the comparisons. Here’s the syntax I’ve settled on for this:
var myArray = [10, 9];
myArray.sort(function (a, b) {
return a > b ? 1 : a < b ? -1 : 0;
});
// myArray is now [9, 10]
The moral of the story: check multiple sources, watch for non-obvious gotchas, and never, ever take candy from strangers.
Short URL to this article:
Tweet this article!
Comments close automatically after 90 days.
Still have something to say? Drop me a line!