Hacktoberfest 2026: the issues maintainers tagged for October, open and beginner-friendly. Browse Hacktoberfest issues

OrderByDescending served by an index returns tied keys in reverse insertion order, unlike LINQ and the non-indexed path

Closed
#210 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 2 days

A pull request for this has already been merged.

  • #244 by @CaffeinatedCoder — closed without merging
  • #323 by @mrdevrobot — merged

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
38/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Active
Tech stack
csharp
Domain
databases

Research direction

The payload names no files, so start by reproducing the mismatch: insert 300 documents with Age = i % 50 and index Age, then compare OrderByDescending(p => p.Age).Take(3) against the non-indexed OrderByDescending(p => p.Age + 0).Take(3). Find the backward index scan in the query path and check how it orders equal keys. The issue leaves open whether to document ties as undefined or match the forward order, so settle that with the maintainer before writing code. Done means both paths agree or the chosen contract is documented and tested.

Written by the indexing model from the issue text.

Description

area/indexing area/linq bug package/core priority/P3 severity/low

Version: main (efacaf4).

What happens

When OrderByDescending is served by a secondary index, documents with equal keys come back in exactly the reverse of the ascending order, i.e. reverse insertion order. LINQ's OrderByDescending is a stable sort and keeps source order for ties, and so does BLite's own non-indexed path (OrderByDescending(p => p.Age + 0)).

300 documents, Age = i % 50, names p000..p299, Age indexed:

query result
OrderByDescending(p => p.Age).Take(3) (index) p149, p299, p099
OrderByDescending(p => p.Age + 0).Take(3) (no index) p249, p199, p049

With Take(n) ending inside a run of ties, the two paths select different documents, not just a different order. Ascending order matched LINQ in all three datasets tried (sequential inserts, ObjectIds in reverse insertion order, relocated documents).

Expected

Either document that ties have no defined order (and accept that the two paths differ), or read each run of equal keys forward when scanning the index backwards so both paths agree.

Dominant language
C#
Stars
66
Forks
6
Avg merge
1d 2h
Merged PRs (30d)
58

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from EntglDb/BLite

All issues in EntglDb/BLite

Similar issues

More C# issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.