Skip to content

fix(tree): sort _id_ index first in the index list#662

Merged
tnaum-ms merged 3 commits into
microsoft:mainfrom
Jacquelinezhong:dev/jacquelinezhong/fix-657-sort-id-index-first
May 27, 2026
Merged

fix(tree): sort _id_ index first in the index list#662
tnaum-ms merged 3 commits into
microsoft:mainfrom
Jacquelinezhong:dev/jacquelinezhong/fix-657-sort-id-index-first

Conversation

@Jacquelinezhong

Copy link
Copy Markdown
Contributor

Summary

Fixes #657.

The _id_ index is the default MongoDB index present on every collection, but it was sorted alphabetically with the rest. Because _ (ASCII 95) sorts after uppercase letters (ASCII 65–90) in many locale comparisons, indexes with uppercase-starting names could appear before _id_, pushing the most fundamental index down the list.

Changes:

  • Extract compareIndexNames(a, b) from the inline sort lambda so the ordering logic is explicit, reusable, and independently testable.
  • _id_ is always returned first (-1); all other indexes fall back to the existing locale-aware, numeric-order sort.
  • Add IndexesItem.test.ts with 8 unit tests covering: _id_ in middle/start/end position, uppercase-named indexes, numeric suffixes, and lists without _id_.

Test plan

  • npx jest src/tree/documentdb/IndexesItem.test.ts – 8/8 tests pass
  • Full unit-test suite (npx jest src/) – 1882/1882 tests pass, 92 suites
  • tsc --noEmit – no type errors
  • eslint src/tree/documentdb/IndexesItem{.ts,.test.ts} – no lint errors

🤖 Generated with Claude Code

The `_id_` index is the default MongoDB index present on every
collection, but it was sorted alphabetically with the rest, so it
could appear anywhere in the list depending on other index names.

Extract `compareIndexNames` and use it in `IndexesItem.getChildren()`
so that `_id_` always sorts to the top, with all other indexes
remaining in case-sensitive, numeric-aware alphabetical order.

Closes microsoft#657

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@Jacquelinezhong Jacquelinezhong requested a review from a team as a code owner May 21, 2026 07:19
Copilot AI review requested due to automatic review settings May 21, 2026 07:19

Copilot AI 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.

Pull request overview

This PR fixes the tree view index ordering so the default _id_ index is always shown first, followed by the remaining indexes sorted by the existing locale-aware numeric comparison.

Changes:

  • Extracts index name ordering into a reusable compareIndexNames(a, b) comparator with _id_ prioritized.
  • Updates IndexesItem.getChildren() to sort using the new comparator.
  • Adds unit tests covering _id_ positioning, uppercase names, numeric suffix ordering, and absence of _id_.

Reviewed changes

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

File Description
src/tree/documentdb/IndexesItem.ts Introduces compareIndexNames and uses it to sort indexes with _id_ first.
src/tree/documentdb/IndexesItem.test.ts Adds unit tests validating the comparator behavior across key ordering scenarios.

Comment thread src/tree/documentdb/IndexesItem.ts
@tnaum-ms

Copy link
Copy Markdown
Collaborator

@Jacquelinezhong Thank you for your contribution! Please address Contributor License Agreement(CLA) instructions above.
Also, will you be able to review the feedback from Copilot?

@tnaum-ms tnaum-ms changed the base branch from next to main May 26, 2026 09:52
@Jacquelinezhong

Copy link
Copy Markdown
Contributor Author

@microsoft-github-policy-service agree

Jacquelinezhong and others added 2 commits May 27, 2026 13:54
When both arguments are the same string (e.g. two '_id_' entries),
the comparator must return 0 rather than -1, since a sort-stability
contract requires equal elements to compare as equal.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@tnaum-ms tnaum-ms added this to the 0.8.1 milestone May 27, 2026

@tnaum-ms tnaum-ms left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Clean, minimal fix. The extracted compareIndexNames comparator is well-structured and the test coverage is thorough. LGTM.

@tnaum-ms tnaum-ms closed this May 27, 2026
@tnaum-ms tnaum-ms reopened this May 27, 2026
@tnaum-ms

Copy link
Copy Markdown
Collaborator

FYI: CodeQL workflow didn't run, closing+opening the PR to trigger it.

@tnaum-ms

Copy link
Copy Markdown
Collaborator

@Jacquelinezhong Thank you for your contribution!
first-contribution

@tnaum-ms tnaum-ms merged commit 181ab8b into microsoft:main May 27, 2026
9 checks passed
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.

Sort _id_ index first in the index list 📌

3 participants