Here's the code. It basically finds the nearest sets of x,y,z points in the nodes array. Since the first column is the point itself, K=2, so that it finds the second nearest point. Then it generates a new array Near which sorts the data based on this new index.
According to the documentation, there's an output argument D that should give the distance. But how do I implement it?