-2

Would this code compromise the guid uniqueness. I am trying to make string value to be sort-able.

Guid.Parse(Now.ToString("yyMMddHH-mmss-fff") & guidi.ToString.Substring(17))

<System.Runtime.CompilerServices.Extension> _
Public Function NewSeqGUID(guidi As Guid) As Guid
    guidi = Guid.Parse(Now.ToString("yyMMddHH-mmss-fff") & guidi.ToString.Substring(17))
    Return guidi
End Function
drewex
  • 317
  • 3
  • 13
  • 1
    The first always throws an exception, and the second doesn't compile, so quesitons about it's uniqueness don't even make sense. You can't create a GUID to begin with using that code, unique or not. – Servy Apr 12 '18 at 21:34
  • 1
    I'm not sure what you are trying to do, but that does not create unique GUID's. – Ron Beyer Apr 12 '18 at 21:34
  • What is this supposed to do? It doesn't work for me at all. Also, `Guid.ToString()` is already sortable. Please post the *actual* code you're using, and what you're intent is – Rufus L Apr 12 '18 at 21:34
  • This is a complete bastardization of the idea of a guid. You can't roll your own guid and still call it a guid. That's entirely against the entire point of a guid. If you want your own sortable id, then just define your id and leave "guid" out of it. – JNevill Apr 12 '18 at 21:37
  • no these would no longer be GUIDs. You probably need a sortable column in your database, which is at the same time the primary key or replication id? Tell us more about the application to lead you out of that dead end. – Cee McSharpface Apr 12 '18 at 21:38
  • But if your question is something like *"If I create a guid from a string based on a time and then immediately create another one based on the time, is it possible they won't be unique?"* then the answer is yes. – Rufus L Apr 12 '18 at 21:38
  • You may be looking for https://msdn.microsoft.com/en-us/library/windows/desktop/aa379322(v=vs.85).aspx to generate sequential GUID-s. What you have now is not a GUID. This question/answer has a C# sample of using it: https://stackoverflow.com/questions/8477664/how-can-i-generate-uuid-in-c-sharp – xxbbcc Apr 12 '18 at 21:39
  • The guidInput and guidi should b GUID.new Guid.NewGuid. its the input value. Sorry forgot to change that. – drewex Apr 12 '18 at 21:42
  • Altered the question to have the full function. – drewex Apr 12 '18 at 21:43
  • but this is, err, visual basic. does VB really allow to call ToString without the call brackets here? – Cee McSharpface Apr 12 '18 at 21:43
  • @ dlatikay this does run without crashing it does work. It doesn't mean its unique tho thats the question. – drewex Apr 12 '18 at 21:52
  • @xxbbcc I might be wrong and please correct me on this. But the sequential GUID coding does not provide string version of it sort-able. – drewex Apr 12 '18 at 21:54

1 Answers1

2

GUIDs are readily sortable. But they are not meant for sorting, and if you do so, you cannot rely on a sorted list of GUIDs to match the order of their creation.
The entropy of their pseudorandomness is implementation-dependent, sometimes even hardware-dependent.

Even the UuidCreateSequential API function's documentation does not promise any sort order to rely on, future implementations may choose to descend instead of ascend, leave gaps, or alternate. Sequence is not the same thing as order.

The strings you create are no longer guaranteed to be unique unless you could enforce that no two timestamps would ever be the same (a good start would be to use four-digit year! remember 2000?)

This is because we do not know where the next GUID will be different from the previous one. If it happens to be in the left part that is overridden by your timestamp, then you could really end up with two identical strings.

One possibility would be to use unique timestamps only. You are free to format those so they look like a GUID and parse into a GUID (for whatever reason). You could use zero padding to fill it up:

{20180413-2213-2490-0000-000000000000}

In the end your question is probably an X-Y problem. Tell us more about what you actually need to achieve, and don't hesitate to add fields, columns, or whatever is needed to achieve both goals: unique keys, and a custom sort order, without compromising the concept of GUID.

Cee McSharpface
  • 8,493
  • 3
  • 36
  • 77
  • Client wants the exported text value sort-able. I went with just 2 year field since the request is for future records and dont think this will last till 3000. Having more alpha and milli seconds in the text value seemed more effective on making it unique. I will just tell them this is not going to happen without compromising the data integrity. – drewex Apr 12 '18 at 22:28
  • I see your dilemma. that is not a reasonable request from your client, they really should have a second column to sort the data. As stated above, you *can* meet their request if you could just add a lookup mechanism to guarantee that timestamps are unique, you don't use it as a replication id or otherwise need to prevent collisions with similar sets of data created somewhere else, and the amount of items does not exhaust the "address space" of all timestamps. – Cee McSharpface Apr 12 '18 at 22:34
  • 1
    The Id can be generated in different systems and no guarantee they will be connected to the server at the moment of creation. The timestamp tick information is stored and which is the sort being used. I get their request they want the ID to be sort-able. But with the cost of duplicate ID's the request isn't doable. – drewex Apr 12 '18 at 22:43