NSArray
has - (NSUInteger)indexOfObject:(id)obj inSortedRange:(NSRange)r options:(NSBinarySearchingOptions)opts usingComparator:(NSComparator)cmp
to determine the insert position of a new object in a sorted array.
What is the best and high-performance way to do this in pure Swift?
Something along the lines of:
var myArray = ["b", "e", "d", "a"]
myArray.sort { $0 < $1 }
// myArray is now [a, b, d, e]
myArray.append("c")
myArray.sort { $0 < $1 }
// myArray is now [a, b, c, d, e]
Instead of appending the new element and then sorting the array, I would like to figure out the correct position and insert the element:
let index = [... how to calculate this index ??? ...]
myArray.insert("c", atIndex: index)
Question&Answers:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…