You're comparing different axes in sort3dbobs()...
Function sort3dbobs() Local noSwap, temp Repeat noSwap = True For j=0 Until maxpoints - 1 If tpoints[opoints[j],2] < tpoints[opoints[j+1],2] temp=opoints[j+1] opoints[j+1]=opoints[j] opoints[j]=temp noSwap = False EndIf Next Until noSwap End Function