I have an array of SCNNode that are sorted by their y positions(Float):
nodesSortedByY = scene.rootNode.childNodes.sorted { $0.position.y > $1.position.y }
What I would like to do is get a new array from nodesSortedByY
where the y values are within a certain range in a similar way to how subscript works but by passing actual values not indexes.
For example:
let nodesSortedByY = [5.0, 4.0, 4.0, 3.0, 2.0, 2.0, 1.0]
let subRange = nodesSortedByY(4.0...2.0)
print(subRange) // [4.0, 4.0, 3.0, 2.0, 2.0]
I tried using indexes originally combined with this binary search but it doesnt work if the values dont exist within the array:
let yPositions = nodesSortedByY.map({ $0.position.y })
let firstIndex = yPositions.binarySearch(forFirstIndexOf: firstValue) ?? 0
let lastIndex = yPositions.binarySearch(forLastIndexOf: lastValue) ?? 0
nodesSortedByY[lastIndex...firstIndex]