0
下面的代碼是從這樣的回答:https://stackoverflow.com/a/28191539/4096655排序添加到陣列包裝類
public class SynchronizedArray<T> {
private var array: [T] = []
private let accessQueue = dispatch_queue_create("SynchronizedArrayAccess", DISPATCH_QUEUE_SERIAL)
public func append(newElement: T) {
dispatch_async(self.accessQueue) {
self.array.append(newElement)
}
}
public subscript(index: Int) -> T {
set {
dispatch_async(self.accessQueue) {
self.array[index] = newValue
}
}
get {
var element: T!
dispatch_sync(self.accessQueue) {
element = self.array[index]
}
return element
}
}
}
var a = SynchronizedArray<Int>()
a.append(1)
a.append(2)
a.append(3)
// can be empty as this is non-thread safe access
println(a.array)
// thread-safe synchonized access
println(a[0])
println(a[1])
println(a[2])
我做的事情很喜歡,但我有麻煩建立一種傳遞到仿製藥的陣列。理想情況下,我想要一個sortInPlace,但不知道如何去做。
我將如何指定對象的屬性進行排序? –
我看到它只是對可比較的符合性中定義的內容進行排序,這將起作用,謝謝! –