亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

按數字按字母順序排序數組

按數字按字母順序排序數組

大話西游666 2019-09-03 16:08:13
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"]


查看完整回答
反對 回復 2019-09-03
  • 3 回答
  • 0 關注
  • 835 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號