A couple of subevents makes it fairly easy. You don't need an array - just two values for 'closest so far' and 'closest UID'. Local variables would be good for those. The algorithm works like this:
1. Set 'closest so far' to a large number e.g. 999999, and 'closest UID' to -1.
2. For each object:
---a) if the distance to this object is less than 'closest so far':
-----> set 'closest so far' to the distance to this object
-----> set 'closest UID' to this object's UID
3. Pick object with UID = 'closest UID'
---> do something with this instance - it's the closest