Swift排序二维数组

时间:2015-07-17 14:49:28

标签: swift

我已声明以下变量:

var Results = [String : Int]()

现在我想根据整数值将此数组从小到大排序。

我试过这个,但它只适用于String值:

func getSmallestResult(fingerprintingResult:[String:Int]) {

    var closestObservationPoint:String = String()

    var myArr = Array(fingerprintingResult.keys)
    var sortedKeys: () = sort(&myArr) {
        var obj1 = fingerprintingResult[$0] // get ob associated w/ key 1
        var obj2 = fingerprintingResult[$1] // get ob associated w/ key 2
        return obj1 < obj2
    }

1 个答案:

答案 0 :(得分:0)

这不是2维array,而是Dictionary。试试这个:

let sortedDictionary = sorted(Results) { $0.0 < $1.0 }
println(sortedDictionary) // [(A, [1, 2]), (D, [5, 6]), (Z, [3, 4])]