title: Sortable UUID v1 for multi-platform application
body: <p>We are looking for a solution to generate a unique id for messages/signals that are exchanged between clients on the Web, iOS, and Android and later persisted on the backend.</p>
  <ul>
  <li><p>Solution have to be standardized</p>
  </li>
  <li><p>available on multiple platforms</p>
  </li>
  <li><p>sortable by created time, indexable by the database</p>
  </li>
  </ul>
  <p>The UUID v1 have these properties except one small thing that sorting and indexing require rearranging of string identifier.</p>
  <p>UUID documentation explains that order of time blocks is reversed(starts from milliseconds) (<a href="https://www.rfc-editor.org/rfc/rfc4122" rel="nofollow noreferrer">link</a>).</p>
  <pre><code>  UUID                   = time-low &quot;-&quot; time-mid &quot;-&quot;
                             time-high-and-version &quot;-&quot;
                             clock-seq-and-reserved
                             clock-seq-low &quot;-&quot; node
    time-low               = 4hexOctet
    time-mid               = 2hexOctet
    time-high-and-version  = 2hexOctet
    clock-seq-and-reserved = hexOctet
    clock-seq-low          = hexOctet
    node                   = 6hexOctet
  </code></pre>
  <p>Because of the UUID representation, we can not sort IDs simply by the string representation of the IDs, and we have to use compare function.</p>
  <pre><code>const toSortableUUID = uuidV1 =&gt;
    uuidV1.replace(/^(.{8})-(.{4})-(.{4})/, '$3-$2-$1');
  
  const uuidCompare = (uuidV1A, uuidV1B) =&gt; {
    if (uuidV1A === uuidV1B) {
      return 0;
    }
    const a = toSortableUUID(uuidV1A);
    const b = toSortableUUID(uuidV1B);
    return a &lt; b ? -1 : 1;
  };
  
  const sortedArrayOfUUIDV1 = arrayOfUUIDV1.concat().sort(uuidCompare);
  </code></pre>
  <p>Do you know another standardized approach that will not have this issue?</p>
  <p>Would it be correct to use UUID v1 but exchange it between clients rearranged so clients can sort by string representation and do not have to use compare function every time for sorting?</p>
  <p>Live test: <a href="https://codesandbox.io/s/q5oRxgnp" rel="nofollow noreferrer">https://codesandbox.io/s/q5oRxgnp</a></p>