Skip to content

Instantly share code, notes, and snippets.

@Joncom
Created July 6, 2016 20:55
Show Gist options
  • Save Joncom/59856f30f351510a656b3ffbf5ec633f to your computer and use it in GitHub Desktop.
Save Joncom/59856f30f351510a656b3ffbf5ec633f to your computer and use it in GitHub Desktop.
Sorting An Array By Multiple Criteria
ig.game.backgroundMaps.sort(function(a,b) {
var o1 = (a.repeat ? 0 : 1);
var o2 = (b.repeat ? 0 : 1);
var p1 = (a.foreground ? 1 : 0);
var p2 = (b.foreground ? 1 : 0);
if(o1 === o2) {
return (p1 < p2) ? -1 : (p1 > p2) ? 1 : 0;
} else {
return (o1 < o2) ? -1 : 1;
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment