Patch function, if Trim=true (default), will attempt to trim the output by “pulling” the biggest curve to the generated surface and use the resulting curve to trim the surface itself.
It will be almost never be flat, so you can’t cap it.
In this solution i just “trim” the un-trimmed patched surface with a planar surface made from the biggest curve.
Added a “sorting” method to find proper level height “index” of every curve from an unsorted list.
Just a side note, you may want to study how data trees work. Your definition should never look like that with all the same procedure copy / pasted multiple times.