Sortable UUID v1 for multi-platform application

We are looking for a solution to create a unique identifier for messages / signals that are exchanged between clients on the web, iOS and Android and then saved to the backend.

  • The solution must be standardized

  • available on multiple platforms

  • sort by time indexed by database

UUID v1 has these properties, with the exception of one small thing that requires reordering the string identifier in order to sort and index.

The UUID documentation explains that the order of time blocks is reversed (starts in milliseconds) ( link ).

  UUID                   = time-low "-" time-mid "-"
                           time-high-and-version "-"
                           clock-seq-and-reserved
                           clock-seq-low "-" node
  time-low               = 4hexOctet
  time-mid               = 2hexOctet
  time-high-and-version  = 2hexOctet
  clock-seq-and-reserved = hexOctet
  clock-seq-low          = hexOctet
  node                   = 6hexOctet

      

Due to the UUID representation, we cannot sort IDs by simply string representation of IDs, and we have to use the compare function.

const toSortableUUID = uuidV1 =>
  uuidV1.replace(/^(.{8})-(.{4})-(.{4})/, '$3-$2-$1');

const uuidCompare = (uuidV1A, uuidV1B) => {
  if (uuidV1A === uuidV1B) {
    return 0;
  }
  const a = toSortableUUID(uuidV1A);
  const b = toSortableUUID(uuidV1B);
  return a < b ? -1 : 1;
};

const sortedArrayOfUUIDV1 = arrayOfUUIDV1.concat().sort(uuidCompare);

      

Do you know of another standardized approach that won't have this problem?

Wouldn't it be correct to use UUID v1, but exchange it between the rearranged clients so that clients can sort by string representations and don't have to use the compare function every time to sort?

Live test: https://codesandbox.io/s/q5oRxgnp

+3


source to share


1 answer


If you change the UUID bit , you no longer have the UUID.

Also note that one of the goals of the UUID standard is to be able to mix the values โ€‹โ€‹of different UUID versions. In other words, as a general rule, you shouldn't assume that your UUIDs are entirely from the same version.

UUIDs were never meant to be broken, never considered a container. Smart programmers who understand this are too smart for their own good.

However, some people change the structure or content of their UUID. I do not recommend this.



Instead, I suggest that you define and separate your concerns.

  • Identifier
    If you need to uniquely identify your objects in time and space without coordination with a centralized server, then use your own UUID.
  • Sort
    If you also want to sort, add another field for the sort value. For example, if you want to sort chronologically, store the timestamp value if supported by your database or data sink. If not supported, save the textual representation of the UTC date / time value in the standard ISO 8601 format . This format is cleverly designed so that when sorted alphabetically, it is also chronological.

2017-01-23T01: 23: 45.123Z

+2


source







All Articles