我正在使用它来对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 }
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句