23.04.2013 Views

javascript

javascript

javascript

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

CHAPTER 6 ■ FUNCTIONS AND ARRAYS<br />

242<br />

} else {<br />

return e1[2] > e2[2] ? -1 : 1;<br />

}<br />

};<br />

console.dir(pirates.sort(sortByLosses));<br />

Figure 6–33. Sorting elements in the pirates array by losses and then by year<br />

Forgive me for being negative and sorting by losses. In Pittsburgh, between the end of the Penguins<br />

season and beginning of the Steelers season, guessing how many games the Pirates will lose and which<br />

star players they’ll trade away for prospects is pretty much all there is to do sports-wise. So let’s be<br />

optimistic and sort pirates by wins instead. Doing so is trivial, just rename the function sortByWins and<br />

change the indexes in the boolean expression and else clause from 2 to 1 like so.<br />

var pirates = [[2010, 57, 105],<br />

[2009, 62, 99],<br />

[2008, 67, 95],<br />

[2007, 68, 94],<br />

[2006, 67, 95],<br />

[2005, 67, 95],<br />

[2004, 72, 89],<br />

[2003, 75, 87],<br />

[2002, 72, 89],<br />

[2001, 62, 100],<br />

[2000, 69, 93],<br />

[1999, 78, 83],<br />

[1998, 69, 93],

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!