当前位置: 动力学知识库 > 问答 > 编程问答 >

ios - Swift May I know How this same thing happens?

问题描述:

Can I know how we can do this in swift ?

Sort NSArray of custom objects based on sorting of another NSArray of strings

I tried with swift's methods

reduce, filter, map

But I am not getting result

网友答案:

Just converted that code to Swift"

func customSortFunction() {
    var firstArray:Array = ["12","10","2"]

    var attribute1 = Attribute(assetID: "10")

    var attribute2 = Attribute(assetID: "2")

    var attribute3 = Attribute(assetID: "12")

    var secondArray = [attribute1, attribute2, attribute3]

    secondArray.sort( { (find(firstArray, $0.assetID!))! < (find(firstArray, $1.assetID!))! } )

    println("\(secondArray[0].assetID), \(secondArray[1].assetID), \(secondArray[2].assetID)")

}

Attribute class:

class Attribute {
var assetID: String?

init(let assetID:String) {
    self.assetID = assetID
}

}

分享给朋友:
您可能感兴趣的文章:
随机阅读: