Use the sketch from this link below as a model:
But change its Comparator::compare() to use dist() instead.