如何按字母顺序对不可变数组进行排序?

时间:2015-05-22 03:13:48

标签: ios arrays swift

如何在Swift中按字母顺序对不可变数组进行排序?这是代码:

let sports = [golf,rugby,football,basketball,soccer,cricket,baseball,softball,handball,volleyball,hockey,lacrosse,swimming,polo,water polo]

这个新排序的数组会按字母顺序显示在表格视图中吗?

2 个答案:

答案 0 :(得分:3)

怎么样:

let sports: [String] = {
    var temp = ["golf","rugby","football","basketball","soccer","cricket","baseball","softball","handball","volleyball","hockey","lacrosse","swimming","polo","water polo"]
    temp.sort(<)
    return temp
}()

这会调用闭包来创建排序数组。如果您在cellForRowAtIndexPath indexPath.row使用TableView索引此数组,则With wsBacktest Set myRangeTimestamp = .Range(Cells(17, 1), Cells(lastRow, 1)) Set myRangeData = .Range(Cells(17, [colMtM]), Cells(lastRow, [colPnL])) End With 将被排序。

答案 1 :(得分:2)

var sports = ["golf","rugby","football","basketball","soccer","cricket","baseball","softball","handball","volleyball","hockey","lacrosse","swimming","polo","water polo"]

sports = sports.sorted {$0.localizedCaseInsensitiveCompare($1) == NSComparisonResult.OrderedAscending }

println(sports)

干杯:)