我试图按长度和位置快速排序一组NSRanges:
var allranges = [NSRange?]()
allranges.append(NSRange(location: 3, length: 5))
allranges.append(NSRange(location: 9, length: 2))
allranges.append(NSRange(location: 9, length: 1))
allranges.append(NSRange(location: 24, length: 5))
allranges.append(NSRange(location: 16, length: 9))
allranges.append(NSRange(location: 11, length: 6))
allranges.sortInPlace{ $0!.location < $1!.location && $0!.length < $1!.length}
print(allranges)
这是我印刷的内容:
[可选((3,5)),可选((9,2)),可选((9,1)),可选((24,5)),可选(( 11,6)),可选((16,9))]
但我希望这样:
[可选((3,5)),可选((9,1)),可选((9,2)),可选((24,5)),可选(( 11,6)),可选((16,9))]
请帮忙!
答案 0 :(得分:1)
您可能希望按location
排序,如果location
相同,则按length
排序,例如:
var allranges = [NSRange]()
allranges.append(NSRange(location: 3, length: 5))
allranges.append(NSRange(location: 9, length: 2))
allranges.append(NSRange(location: 9, length: 1))
allranges.append(NSRange(location: 24, length: 5))
allranges.append(NSRange(location: 16, length: 9))
allranges.append(NSRange(location: 11, length: 6))
allranges.sortInPlace { $0.location == $1.location ? $0.length < $1.length : $0.location < $1.location }