Finding the next smallest value of LayoutOrder

Hello, I’m trying to write a script that determines the next lowest LayoutOrder number from a set of frames in a GUI.

Suppose I have four frames (# of frames is dynamic), with the same parent, with the LayoutOrders of 4,5,8, and 9 (layout orders are dynamic).
Number 8 is the active frame (frame.Active = true)

I need to figure out what the next lowest number, if any, after that is, and then make that the active frame. In this case it would be the one with the LayoutOrder of 5.

What functions are available to me to make this less convoluted?

i suppose you can add them to a linked list when you add the frames and have their head be the value of the layour order and their pointer be the one that is higher than them.