我正在使用它在 Swift 中对数组中的组件进行排序:
myArray = myArray.sorted { $0.localizedCaseInsensitiveCompare($1) == NSComparisonResult.OrderedAscending }
但是,它给了我以下结果:
[18C, 18L, 18R, 22, 24, 27, 36C, 36L, 36R, 4, 6, 9]
是否有可能以正确的方式对其进行排序,即
[4, 6, 9, 18C, 18L, 18R, 22, 24, 27, 36C, 36L, 36R]
最佳答案
您可以将 compare
与 .NumericSearch
一起使用:
array.sortInPlace { $0.compare($1, options: .NumericSearch) == .OrderedAscending }
或
let array2 = array.sort { $0.compare($1, options: .NumericSearch) == .OrderedAscending }
关于arrays - Swift:数组排序未正确完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26954029/