Here is my approach .
Be careful with scenarios such as the attached example
Correct , I did not consider the text value since that it DOES represent the Z coordinate with its coordinates ( the text object ) as shown in OP's attached drawing .
Though that I admit it should account for every eventuality
Thank you .
Oh, I hadn't actually noticed that the text had elevation of its own - usually such tasks involve using the text content as the elevation value...
My sample drawing was more illustrating the shortfalls of sorting the text objects by their distance from the polyline start point, which will produce undesired results for polylines which loop back on themselves.
Lee