简体   繁体   中英

How to sort NSOrderedSet by date in Swift

I'm trying to sort a NSOrderedSet of an entity called Log which has 3 attributes (logDate, logDescription, logThumbnail) by date using NSSortDescriptor but it doesn't work.

if let logs = projectSelected?.logs {
        let sd = NSSortDescriptor(key: "logDate", ascending: false)
        logs.sortedArrayUsingDescriptors([sd])
}

I have also tried to use projectLogs?.sortInPlace{$0.logDate!.compare($1.logDate!) == .OrderedAscending} to no success. What is the best solution for what I'm trying to do? Thank you in advance for your help.

To sort an NSOrderedSet in place it must be mutable ( NSMutableOrderedSet )

let mutableLogs = logs.mutableCopy()
let sd = NSSortDescriptor(key: "logDate", ascending: false)
mutableLogs.sortUsingDescriptors([sd])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM