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條經驗 獲得超14個贊
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 關注
- 835 瀏覽
添加回答
舉報
0/150
提交
取消