I am creating and using time base UUID for items. I am using this function:
class func timeBasedUUID() -> NSUUID {
let uuidSize = sizeof(uuid_t)
let uuidPointer = UnsafeMutablePointer<UInt8>.alloc(uuidSize)
uuid_generate_time(uuidPointer)
let uuid = NSUUID(UUIDBytes: uuidPointer)
uuidPointer.dealloc(uuidSize)
return uuid
}
And now I would like to know how can I sort them from oldest to newest. I was thinking about comparing strings but it is correct? Because I don't think so.
UUIDs have no "oldest" or "newest" component. It's true that uuid_generate_time
uses the time to help guarantee uniqueness, but it is not intended as a way of actually recording the time. Don't try to misuse it in this way. If you know you're going to need to sort items by date, you need to give them an additional bit of information, e.g. a timestamp.