If there are multiple nearest targets, caused by them being precisely the same distance away, targets are sorted by the time they have been on the server (since their most recent join for players or their creation for other entities) with the longest times selected first. For example, if equally distant, @a[c=1] will select the player who has been on the server the longest and @e[type=creeper,c=3] will select the three oldest creepers.