myArray = [Step 6, Step 12, Step 5, Step 14, Step 4, Step 11, Step 16, Step 9,
Step 3, Step 13, Step 8, Step 2, Step 10, Step 7, Step 1, Step 15]
Run Code Online (Sandbox Code Playgroud)
如何以这种方式对上面的数组进行排序?
[Step 1, Step 2, Step 3, Step 4, ....]
Run Code Online (Sandbox Code Playgroud)
我在swift中使用了这个函数,sort(&myArray,{ $0 < $1 })但它是这样排序的
[Step 1, Step 10, Step 11, Step 12, Step 13, Step 14, Step 15, Step 16, Step 2,
Step 3, Step 4, Step 5, Step 6, Step 7, Step 8, Step 9]
Run Code Online (Sandbox Code Playgroud)
Mar*_*n R 43
另一种变体是使用
localizedStandardCompare:.从文档:
只要文件名或其他字符串出现在适合类似Finder的排序的列表和表中,就应该使用此方法.
这将根据当前语言环境对字符串进行排序.例:
let myArray = ["Step 6", "Step 12", "Step 10"]
let ans = sorted(myArray,{ (s1, s2) in
return s1.localizedStandardCompare(s2) == NSComparisonResult.OrderedAscending
})
println(ans)
// [Step 6, Step 10, Step 12]
Run Code Online (Sandbox Code Playgroud)
更新:上面的答案很老,对于Swift 1.2.一个Swift 3版本(感谢@Ahmad):
let ans = myArray.sorted {
(s1, s2) -> Bool in return s1.localizedStandardCompare(s2) == .orderedAscending
}
Run Code Online (Sandbox Code Playgroud)
有关其他方法,请参阅/sf/answers/2184683441/,转换为Swift 3,网址为/sf/answers/2782407421/.
Mar*_*n R 12
Swunc 3版本的Duncan C的答案是
let myArray = ["Step 6", "Step 12", "Step 10"]
let sortedArray = myArray.sorted {
$0.compare($1, options: .numeric) == .orderedAscending
}
print(sortedArray) // ["Step 6", "Step 10", "Step 12"]
Run Code Online (Sandbox Code Playgroud)
或者,如果要对数组进行就地排序:
var myArray = ["Step 6", "Step 12", "Step 10"]
myArray.sort {
$0.compare($1, options: .numeric) == .orderedAscending
}
print(myArray) // ["Step 6", "Step 10", "Step 12"]
Run Code Online (Sandbox Code Playgroud)
NSString有一组丰富的比较字符串的方法.您可以使用该方法compare:options:.
你会想要这个NSNumericSearch选项.如果您阅读该选项的说明,它会说:
字符串中的数字使用数值进行比较,即Name2.txt <Name7.txt <Name25.txt.
因此,您可以编写一个Swift排序行,使用该函数执行您想要的操作.
像这样的东西:
let myArray = ["Step 6", "Step 12", "Step 10"]
let ans = myArray.sorted {
(first, second) in
first.compare(second, options: .NumericSearch) == NSComparisonResult.OrderedAscending
}
println(ans)
// [Step 6, Step 10, Step 12]
Run Code Online (Sandbox Code Playgroud)
在Swift 2中:
import Foundation
let myArray = ["Step 6", "Step 12", "Step 10"]
extension String {
func extractIntFromEnd() -> Int? {
return self.componentsSeparatedByString(" ").last.flatMap{Int($0)}
}
}
let ans = myArray.sort {
(first, second) in
first.extractIntFromEnd() < second.extractIntFromEnd()
}
Run Code Online (Sandbox Code Playgroud)
在Swift 1中:
let myArray = ["Step 6", "Step 12", "Step 10"]
extension String {
func extractIntFromEnd() -> Int? {
return self.componentsSeparatedByString(" ").last.flatMap{$0.toInt()}
}
}
let ans = myArray.sorted {
(first, second) in
first.extractIntFromEnd() < second.extractIntFromEnd()
}
Run Code Online (Sandbox Code Playgroud)
在这两个中,这个数组:
let myArray = [
"Step 6" ,
"Step 12",
"Step 5" ,
"Step 14",
"Step 4" ,
"Step 11",
"Step 16",
"Step 9" ,
"Step 3" ,
"Step 13",
"Step 8" ,
"Step 2" ,
"Step 10",
"Step 7" ,
"Step 1" ,
"Step 15"
]
Run Code Online (Sandbox Code Playgroud)
会给你这个答案:
["Step 1", "Step 2", "Step 3", "Step 4", "Step 5", "Step 6", "Step 7", "Step 8", "Step 9", "Step 10", "Step 11", "Step 12", "Step 13", "Step 14", "Step 15", "Step 16"]
Run Code Online (Sandbox Code Playgroud)
(在Swift 2.0版本中,您应该可以这样做last.flatMap(Int.init),但由于某种原因它不适合我.甚至类似的东西["1", "2"].flatMap(Int.init)都会让我的操场崩溃.看起来像个bug.)