frontendfriends/node-combine-mq — pull request #5 by kinglozzer (2014-12-12 10:25:16+00:00)
Title: fix(Task): Stabilise sort order for media queries

This had me tearing my hair out. On a larger site with multiple media queries at the same width, but with different pixel densities, the results of combining the queries would be returned in a random sort order. I.e:

`min-width(480px)`
`min-width(480px) and (min-device-pixel-ratio: 2)`
`min-width(480px) and (min-device-pixel-ratio: 3)`

After combining multiple examples of these, the order of the result could be anything (meaning the first media query might override the third, rendering it useless). I eventually tracked it down to `Array.prototype.sort`, which was comparing the queries in a random order because it’s not a “stable” sort. After this change if the computed `sortVal`s are equal, we fall back to sorting by their original position in the list of media queries (rather than returning 0 which results in a random order).

According to [this issue](https://code.google.com/p/v8/issues/detail?id=90), this will only affect stylesheets with more than 10 of one type of media query as the V8 engine’s sort is stable up until that number.