Skip to content

Improve sorting of exercises when adding to assignment via book browse - #1403

Open
oscarlevin wants to merge 2 commits into
RunestoneInteractive:mainfrom
oscarlevin:exercise-sort
Open

Improve sorting of exercises when adding to assignment via book browse#1403
oscarlevin wants to merge 2 commits into
RunestoneInteractive:mainfrom
oscarlevin:exercise-sort

Conversation

@oscarlevin

Copy link
Copy Markdown
Collaborator

Currently if an instructor tries to add exercises to an assignment by browsing problems in the book, when navigating to a section, they get the exercises sorted by exercise name, which might be something like "Exercise 1.3.2" or "Exercise 1.3.11". But these are sorted as strings, so the 11th exercise appears before then 2nd.

This pr fixes that by adding a natural sort key function that sorts them "correctly"

@oscarlevin
oscarlevin requested a review from bnmnetp as a code owner August 21, 2026 21:22
Copilot AI lite review requested due to automatic review settings August 21, 2026 21:22

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 incorrect lexicographic ordering of exercise identifiers when instructors browse book sections to add exercises to an assignment, by introducing a natural-sort key and re-sorting questions per subchapter after the DB query.

Changes:

  • Added a _natural_sort_key helper to compare numeric runs numerically (e.g., 3.2.1 before 3.10.1).
  • Re-sorted each subchapter’s question list in Python using the natural sort key to override DB string ordering.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread components/rsptx/db/crud/question.py
Comment on lines +832 to +836
for chapter_questions in questions.values():
for subchapter_questions in chapter_questions.values():
subchapter_questions.sort(
key=lambda q: _natural_sort_key(q.qnumber or q.name)
)
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings August 21, 2026 21:48

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

Copilot reviewed 1 out of 1 changed files in this pull request and generated no new comments.

Suppressed comments (1)

components/rsptx/db/crud/question.py:837

  • This introduces new ordering logic for chapter/subchapter question lists but there’s no regression test covering the natural sort behavior (e.g., ensuring "Exercise 1.3.2" sorts before "Exercise 1.3.11"). Since this endpoint drives assignment creation UI, adding a focused test would help prevent future regressions.
        for chapter_questions in questions.values():
            for subchapter_questions in chapter_questions.values():
                subchapter_questions.sort(
                    key=lambda q: _natural_sort_key(q.qnumber or q.name)
                )

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