Need Help ! Pick elements from a list with condition

Hi everyone,
Now I’m doing some work related to precast installation. I need to define which intersection point of grid lines is closest to the element. In the script I’ve already got the distance from a center point of element to nearest grid line point but I don’t know how to select the element accordingly to assign parameter name. Please help me to solve that.
If anyone has better solution to define the closest grid line intersection to element please enlighten me
Thanks in advance

Have you tried a sortbykey node?

Take the elements from select model elements using gridlines and insert it as the list, with the cuboid.length node as the keys.

If I understand correctly, it should automatically sort by lowest to highest.