fix(tree): sort _id_ index first in the index list#662
Conversation
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>
There was a problem hiding this comment.
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. |
|
@Jacquelinezhong Thank you for your contribution! Please address Contributor License Agreement(CLA) instructions above. |
|
@microsoft-github-policy-service agree |
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
left a comment
There was a problem hiding this comment.
Clean, minimal fix. The extracted compareIndexNames comparator is well-structured and the test coverage is thorough. LGTM.
|
FYI: CodeQL workflow didn't run, closing+opening the PR to trigger it. |
|
@Jacquelinezhong Thank you for your contribution! |

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:
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.IndexesItem.test.tswith 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 passnpx jest src/) – 1882/1882 tests pass, 92 suitestsc --noEmit– no type errorseslint src/tree/documentdb/IndexesItem{.ts,.test.ts}– no lint errors🤖 Generated with Claude Code