The previous developer used a mind-numbing tangle of recursion to try to solve this problem, and it didn't work, so I took it over and straightened out the sorting. I'd love to know if there was a better way to approach it from the beginning than to do all these mental gymnastics to convert their business logic into something NetSuite can understand. XD