问题描述:

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
}

}

相关阅读:
Top