Some glasses will make this easy with the methods they provide. E.g.
nearest_item = self.findNearest(self.findItems())
self.move(nearest_item.pos)
But it’s still possible with the methods you have available. E.g.
def compare_distance(item1, item2):
if distance(item1) < distance(item2):
return -1
elif distance(item1) > distance(item2):
return 1
else:
return 0
nearest_items = sorted(self.getItems(), cmp=compare_distance)
The comparison function can be simplified since it needs a positive, negative, or zero value (not necessarily +1, -1, and 0) to check if it’s greater, lower, or equal.
def compare_distance(item1, item2):
return distance(item1) - distance(item2)