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]
3 回答
翻翻过去那场雪
TA贡献2065条经验 获得超13个赞
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"]
或者,如果要对数组进行就地排序:
var myArray = ["Step 6", "Step 12", "Step 10"]
myArray.sort {
$0.compare($1, options: .numeric) == .orderedAscending
}
print(myArray) // ["Step 6", "Step 10", "Step 12"]
- 3 回答
- 0 关注
- 793 浏览
添加回答
举报
0/150
提交
取消