How can I calculate the distance between sprites and let them glide towards the second closest one?

This example might help with regard to calculating 'distance' between sprites.

Remember that the x,y coordinates reported for a sprite are the upper left hand corner of the sprite unless you write some code to determine the 'center' of the sprite.