Grasshopper contour and sort list

Hi I’d like to sort group of lines With a range of z-axis

For example, I want to extract lines 3 to 17 from the top of line5

I used sort list from z vector and list item but it doesnt work.
How do I write a script?
Thanks

Rheotomic_Surfaces 0509.gh (32.5 KB)

The outputs from Contour are already sorted in the Z direction, just have to select which branches you want.

With a simple tree structure like this, you can also use Flip Matrix
image

Thanks for the detailed explanation.