我用它来在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]
答案 0 :(得分:8)
您可以将compare
与.NumericSearch
:
array.sortInPlace { $0.compare($1, options: .NumericSearch) == .OrderedAscending }
或
let array2 = array.sort { $0.compare($1, options: .NumericSearch) == .OrderedAscending }