Skip to content

Sort URLSearchParams by code units and keep equal names in order - #1415

Merged
FlorianRappl merged 2 commits into
AngleSharp:develfrom
menes97:fix/url-search-params-sort-ordinal
Nov 18, 2026
Merged

FlorianRappl merged 2 commits into
AngleSharp:develfrom
menes97:fix/url-search-params-sort-ordinal

Conversation

@menes97

@menes97 menes97 commented Nov 18, 2026

Copy link
Copy Markdown
Contributor

Types of Changes

Prerequisites

  • I have read the CONTRIBUTING document
  • My code follows the code style of this project

Contribution Type

  • Bug fix (non-breaking change which fixes an issue, please reference the issue id)
  • New feature (non-breaking change which adds functionality, make sure to open an associated issue first)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • My change requires a change to the documentation
  • I have updated the documentation accordingly
  • I have added tests to cover my changes
  • All new and existing tests passed

Description

There is no issue for this one; I found it while running the tests on a machine set to the Turkish culture.

UrlSearchParams.Sort() used _values.Sort((a, b) => a.Key.CompareTo(b.Key)), which differs from the URL Standard's sort() in two ways:

  • The standard sorts the names by comparison of code units. String.CompareTo uses the current culture, so under tr-TR (and other cultures) a sorts before A, where code-unit order puts A (U+0041) first.
  • The standard keeps the relative order of pairs with equal names. List<T>.Sort is not stable, so with more than 16 pairs, pairs with the same name come out reordered.

The fix sorts with OrderBy(p => p.Key, StringComparer.Ordinal), which is stable and compares code units, and writes the result back into the list.

Tests (in UrlSearchParamsIteration):

  • SortOrdersTheNamesByCodeUnitsNotByCulture: fails before the change (Expected "A" but was "a" on this tr-TR machine).
  • SortKeepsTheOrderOfPairsWithTheSameName: fails before the change (Expected 1 but was 19).

Both pass with the change. The full AngleSharp.Core.Tests run on net10.0 passes (4881 passed, 1 skipped, same skip as before). I did not run the net462/net472 targets.

This change was written with Claude Code.

🤖 Generated with Claude Code

URLSearchParams.sort() compared the names with the current culture and used an unstable sort. The standard sorts by UTF-16 code units and keeps the relative order of pairs with the same name.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@CLAassistant

CLAassistant commented Nov 18, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@FlorianRappl FlorianRappl left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This is fine - but it misses a CHANGELOG entry for 1.10.5 (as well as other version changes package.json and the Directory.Build.props).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@menes97

menes97 commented Nov 18, 2026

Copy link
Copy Markdown
Contributor Author

Thanks, added in ad385b0: a 1.10.5 section in the CHANGELOG with this fix, and the version bumped to 1.10.5 in src/Directory.Build.props and src/AngleSharp.Core.Docs/package.json.

@FlorianRappl
FlorianRappl merged commit 2648889 into AngleSharp:devel Nov 18, 2026
5 checks passed
This was referenced Nov 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants