Skip to content

Instantly share code, notes, and snippets.

@bnoordhuis
Created July 31, 2012 14:41
Show Gist options
  • Save bnoordhuis/3217499 to your computer and use it in GitHub Desktop.
Save bnoordhuis/3217499 to your computer and use it in GitHub Desktop.
var a = [0, 5, 2, 1, 3];
var p = 2; // pivot
function diff(a, b) {
return Math.abs(a - b);
}
function sort(a, b) {
var ax = diff(a, p);
var bx = diff(b, p);
if (ax > bx) return 1;
if (ax < bx) return -1;
return 0;
}
console.log(a.sort(sort));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment