Skip to content

Fix SequentialSqlGuid in-memory sorting#20

Merged
buvinghausen merged 3 commits intomasterfrom
fix_sequentialsqlguid_sort
Mar 21, 2026
Merged

Fix SequentialSqlGuid in-memory sorting#20
buvinghausen merged 3 commits intomasterfrom
fix_sequentialsqlguid_sort

Conversation

@buvinghausen
Copy link
Copy Markdown
Owner

Fix SequentialSqlGuid in-memory sorting

Problem

SequentialSqlGuid.CompareTo was delegating to Guid.CompareTo, which uses standard byte-order comparison. SQL Server sorts uniqueidentifier values using a different byte-order priority, so in-memory sorting of SequentialSqlGuid did not match the order SQL Server would produce.

Fix

SequentialSqlGuid now wraps calls through System.Data.SqlTypes.SqlGuid to ensure CompareTo, Equals, and GetHashCode all use SQL Server–compatible byte ordering:

public int CompareTo(SequentialSqlGuid other) =>
    new SqlGuid(Value).CompareTo(new SqlGuid(other.Value));

public bool Equals(SequentialSqlGuid other) =>
    new SqlGuid(Value).Equals(new SqlGuid(other.Value));

public override int GetHashCode() =>
    new SqlGuid(Value).GetHashCode();

The CompareTo(object) overload also now accepts SqlGuid directly.

Tests

  • Added SortedGuidList / SortedSqlGuidList sort-order verification tests to both GuidV7Tests and GuidV8TimeTests, confirming that SequentialGuid and SequentialSqlGuid collections sort in the expected byte order with version/variant bits and a known timestamp.
  • Tightened existing ShouldBe assertions to use ignoreOrder: false so sort-order regressions are caught explicitly.
  • Changed test list types from IList<T> to static IReadOnlyList<T> in SequentialGuidTests for consistency.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes SequentialSqlGuid’s in-memory comparison semantics so sorting matches SQL Server uniqueidentifier ordering by delegating comparisons (and related equality/hash behavior) to System.Data.SqlTypes.SqlGuid. It also adds/adjusts tests to ensure sort-order regressions are caught explicitly.

Changes:

  • Update SequentialSqlGuid.CompareTo(SequentialSqlGuid), Equals, and GetHashCode to use SqlGuid for SQL Server–compatible semantics.
  • Expand CompareTo(object) to accept SqlGuid directly.
  • Add/adjust tests in v7/v8 and core test suites to verify ordering with ignoreOrder: false and to validate timestamp extraction for known vectors.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

File Description
src/SequentialGuid/SequentialSqlGuid.cs Routes comparison/equality/hash behavior through SqlGuid to align with SQL Server ordering.
test/SequentialGuid.Tests/SequentialGuidTests.cs Makes ordering assertions explicit (ignoreOrder: false) and standardizes sorted-list types.
test/SequentialGuid.Tests/GuidV7Tests.cs Adds explicit sorted-list order verification for SequentialGuid and SequentialSqlGuid v7 vectors.
test/SequentialGuid.Tests/GuidV8TimeTests.cs Adds explicit sorted-list order verification for SequentialGuid and SequentialSqlGuid v8 time vectors.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@buvinghausen buvinghausen merged commit 1811dfd into master Mar 21, 2026
1 check passed
@buvinghausen buvinghausen deleted the fix_sequentialsqlguid_sort branch March 21, 2026 20:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants