Skip to content

Instantly share code, notes, and snippets.

@demux
Created December 19, 2013 16:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save demux/8042026 to your computer and use it in GitHub Desktop.
Save demux/8042026 to your computer and use it in GitHub Desktop.
function nearestInList(number, list, returnDistance) {
var distance = Infinity;
var n = null;
for(var i in list) {
var _distance = Math.abs(list[i] - number);
if(_distance < distance) {
distance = _distance;
n = list[i];
}
}
if(returnDistance === true) {
return distance;
}
return n;
}
function nearSort(n, list) {
return lst.sort(function(a, b) {
return Math.abs(a - n) - Math.abs(b - n);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment