Question

What is the best and FASTEST way to sort a UUID v1 list:

ss = [(uuid1(), i, time.sleep(i)) for i in range(10)]

ss
[(UUID('9a5e8b20-be62-11e3-87fa-0800270bf8cf'), 0, None), (UUID('9a5e9246-be62-11e3-87fa-0800270bf8cf'), 1, None), (UUID('9af75166-be62-11e3-87fa-0800270bf8cf'), 2, None), (UUID('9c28d4ba-be62-11e3-87fa-0800270bf8cf'), 3, None), (UUID('9df31a58-be62-11e3-87fa-0800270bf8cf'), 4, None), (UUID('a056198a-be62-11e3-87fa-0800270bf8cf'), 5, None), (UUID('a351c986-be62-11e3-87fa-0800270bf8cf'), 6, None), (UUID('a6e63dc0-be62-11e3-87fa-0800270bf8cf'), 7, None), (UUID('4f418380-cfac-11e3-87fa-0800270bf8cf'), 8, None), (UUID('7de51c1e-d075-11e3-87fa-0800270bf8cf'), 9, None)]

It is not always giving correct results using the following statement:

sorted(ss, key= lambda x: x[0])

[(UUID('4f418380-cfac-11e3-87fa-0800270bf8cf'), 8, None), (UUID('7de51c1e-d075-11e3-87fa-0800270bf8cf'), 9, None), (UUID('9a5e8b20-be62-11e3-87fa-0800270bf8cf'), 0, None), (UUID('9a5e9246-be62-11e3-87fa-0800270bf8cf'), 1, None), (UUID('9af75166-be62-11e3-87fa-0800270bf8cf'), 2, None), (UUID('9c28d4ba-be62-11e3-87fa-0800270bf8cf'), 3, None), (UUID('9df31a58-be62-11e3-87fa-0800270bf8cf'), 4, None), (UUID('a056198a-be62-11e3-87fa-0800270bf8cf'), 5, None), (UUID('a351c986-be62-11e3-87fa-0800270bf8cf'), 6, None), (UUID('a6e63dc0-be62-11e3-87fa-0800270bf8cf'), 7, None)]

The Expected order is: 0,1,2,3,4,5,6,7,8,9 and not 8,9,0,1,2,3,4,5,6,7

Note that the list I want to sort has only UUIDs: sorting by x[1] in the above example won't work for me.

Was it helpful?

Solution

Python's UUIDs are sorted according to the int property which is the only member of a UUID object.

def __cmp__(self, other):
    if isinstance(other, UUID):
        return cmp(self.int, other.int)
    return NotImplemented

This is different from the "timestamp" part of the UUID v1 object (which you can access as the time property) because they refer to the underlying byte sequnce with different orders:

>>> x = uuid.uuid1()
>>> x
UUID('488085a8-be83-11e3-a999-1803733f0b7d')
>>> x.int
96371740624175594482388131032705469309L
>>> ((((((((((x.time_low << 16) | x.time_mid) << 16) | x.time_hi_version) << 8) | x.clock_seq_hi_variant) << 8) | x.clock_seq_low) << 48) | x.node) == x.int
True
>>> x.time
136161884817622440L
>>> (((((x.time_hi_version & 0xFFF) << 16) | x.time_mid) << 32) | x.time_low) == x.time
True

So you need to specify the "time" property when you sort UUID v1 objects according to thier timestamps:

sorted(ss, key= lambda x: x[0].time)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top