猿问

按数字按字母顺序排序数组

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]

如何以这种方式对上面的数组进行排序?


[Step 1, Step 2, Step 3, Step 4, ....]

我在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]


大话西游666
浏览 803回答 3
3回答

翻翻过去那场雪

Swunc 3版本的Duncan C的答案是let myArray = ["Step 6", "Step 12", "Step 10"]let sortedArray = myArray.sorted {&nbsp; &nbsp; $0.compare($1, options: .numeric) == .orderedAscending}print(sortedArray) // ["Step 6", "Step 10", "Step 12"]或者,如果要对数组进行就地排序:var myArray = ["Step 6", "Step 12", "Step 10"]myArray.sort {&nbsp; &nbsp; $0.compare($1, options: .numeric) == .orderedAscending}print(myArray) // ["Step 6", "Step 10", "Step 12"]
随时随地看视频慕课网APP
我要回答