I measured whether .NET Guid sorting is broken. It isn't.
" Guid sorting in .NET is weird" is a claim you run into often enough that I wanted a number for it. So I measured CompareTo directly. It sorts in logical value order. The claim is wrong, and the thing people are actually hitting is something else. You need a sample that can tell the two hypotheses apart This is the part that took me two attempts. .NET stores the first three groups of a Guid in…
A recent investigation sought to determine whether sorting .NET Guids is broken. As it turns out, the claim is incorrect, and the issue lies elsewhere. The key to resolving this matter lies in conducting a proper test that can distinguish between different hypotheses.
To begin, the investigator measured the CompareTo method of .NET Guids directly. It was found that Guid.CompareTo sorts in logical value order, disproving the initial claim. However, the real challenge was to create a sample that could differentiate between the two hypotheses: sorting by string order versus sorting by raw byte order.
The test required values that would demonstrate the disagreement between these two sorting methods. The ToByteArray() method was utilized to convert the Guid into bytes, with the first four bytes being displayed in both little-endian and big-endian formats. Through this process, it was revealed that .NET stores the first three groups of a Guid in little-endian internally.
The crucial finding was that sorting by string order and sorting by raw byte order yielded different results. When sorted by string, the values came out in one order, while sorting by raw bytes resulted in the exact reverse order. This highlighted the importance of distinguishing between the two hypotheses.
The third line of the investigation, which compared the CompareTo order with the raw byte order, proved to be the most significant. Without this comparison, it would be impossible to determine whether a passing test was actually due to the inability of the test to fail. Thus, it was established that .NET Guid.CompareTo is independent of the ToByteArray() layout.
The source of the confusion lies in the fact that the byte layout returned by ToByteArray() does not adhere to RFC 9562 network byte order. Instead, the first three groups of the Guid are reversed. This discrepancy becomes evident when comparing the first four bytes, with the reversed order of the first three groups clearly visible.
Moving forward, it is essential to create a sample that can effectively differentiate between the two hypotheses. Python provides both the RFC order and the default layout of .NET Guids, labeling them as .bytes and .bytes_le, respectively. SQL Server utilizes a different ordering scheme, which contributes to the majority of "sorting is weird" stories.
In conclusion, sorting .NET Guids is not broken. The layout differences only become apparent when a Guid is serialized and read back using a different library. To avoid any confusion, it is recommended to explicitly specify the desired ordering when transmitting a Guid across system boundaries. The findings of this investigation were verified on .NET 10.0.400, with the same results obtained on .NET 10.0.11 from an earlier test conducted on 2026-09-14.
For a comprehensive analysis, including the Python and SQL Server aspects, please refer to the full write-up available at https://uuid.withuse.io/uuid-vs-guid/.
Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.