Project

General

Profile

Actions

Defect #43524

open

Incorrect sorting when using "Subproject of" (parent project) as primary sort key in Project List view

Added by ak iwasaki 5 months ago. Updated 1 day ago.

Status:
New
Priority:
Normal
Assignee:
-
Category:
Projects
Target version:
-
Resolution:
Affected version:

Description

In the Project List view, when "Subproject of" (parent project) field is included as the primary sort key and the other field as a secondary key, the sorting result becomes incorrect.

Cause:

In models/project_query.rb line 42
QueryColumn.new(:parent_id, :sortable => "#{Project.table_name}.lft ASC", :default_order => 'desc', :caption => :field_parent),

However, #{Project.table_name}.lft ASC represents the hierarchical structure itself. Even projects with the same name can have different lft values, and when this is included as a sort key, a secondary sort key does not work correctly.

The following definition appears to be correct:

QueryColumn.new(:parent_id, :sortable => "#{Project.table_name}.parent_id", :default_order => 'desc', :caption => :field_parent),

Files

patch-issue-43524-fix-project-sort-by-parent.diff (2.16 KB) patch-issue-43524-fix-project-sort-by-parent.diff Akihiro Kubota, 2026-05-05 07:14
after_sort_asc.png (235 KB) after_sort_asc.png Akihiro Kubota, 2026-05-05 07:14
before_sort_asc.png (240 KB) before_sort_asc.png Akihiro Kubota, 2026-05-05 07:14
before_sort_desc.png (243 KB) before_sort_desc.png Akihiro Kubota, 2026-05-05 07:14
after_sort_desc.png (243 KB) after_sort_desc.png Akihiro Kubota, 2026-05-05 07:14
patch-issue-43524-fix-project-sort-by-parent-v2.diff (2.77 KB) patch-issue-43524-fix-project-sort-by-parent-v2.diff Akihiro Kubota, 2026-05-05 10:28
Actions #1

Updated by ak iwasaki 5 months ago

Japanese translation
プロジェクト リストで,"親プロジェクト名"を条件に含む並び替えを行った場合,2つ目以降に指定した条件が無視されて正しくソートされません。

models/project_query.rb line 42行目
QueryColumn.new(:parent_id, :sortable => "#{Project.table_name}.lft ASC", :default_order => 'desc', :caption => :field_parent),

の#{Project.table_name}.lftは,それ自体が階層構造持っていて,同じ親プロジェクト名であっても違う値になり,それに基づきソートされるため2つ目以降の条件が意図するソートになりません。

#{Project.table_name}.parent_id が正しい記述と思われます。

Actions #2

Updated by Akihiro Kubota 1 day ago

This is my first contribution to Redmine, so I apologize in advance if anything is incorrect or missing.

I have created a patch based on the fix proposed in the description.

Fix:
Changed the sortable value for the parent_id column in app/models/project_query.rb from "#{Project.table_name}.lft ASC" to "#{Project.table_name}.parent_id".

The embedded ASC in the original lft ASC string caused Redmine's append_order method to treat it as a fixed-direction expression, ignoring the user-selected sort direction entirely. As a result, both ascending and descending clicks produced the same order.

Tests added:

test_sort_by_parent_id_asc_should_not_include_fixed_direction — verifies ASC direction is applied and lft is no longer used
test_sort_by_parent_id_desc_should_apply_desc_direction — verifies DESC direction is now correctly applied (this failed before the fix)
Screenshots showing the before/after behavior are attached.

Actions #3

Updated by Akihiro Kubota 1 day ago

I found that the previous patch I submitted introduced a regression in
`test_index_as_list_should_indent_projects`. I have revised the fix and attached
`patch-issue-43524-fix-project-sort-by-parent-v2.diff`.

Root cause:
The `sortable` value `"#{Project.table_name}.lft ASC"` has a hardcoded `ASC` suffix,
which causes Redmine's `append_order` to treat it as a fixed-direction expression and
ignore the user-selected sort direction entirely.

Why not use `parent_id`:
Changing to `parent_id` fixed the direction bug but broke project indentation.
`grouped_project_list` in `projects_helper.rb` relies on tree traversal order
(parents before children) to compute indent depth. Sorting by `parent_id` does not
guarantee this order.

Fix:
In `app/models/project_query.rb`, changed `sortable` from `"#{Project.table_name}.lft ASC"`
to `"#{Project.table_name}.lft"` (removing the hardcoded `ASC` suffix) and `default_order`
from `'desc'` to `'asc'`.
This allows the user-chosen direction to be applied while preserving tree order for
proper indentation.

Tests:
- `test/functional/projects_controller_test.rb`: Updated `test_index_as_list_should_indent_projects`
to use `parent_id:asc` (ASC maintains tree traversal order, so indentation works correctly)
- `test/unit/project_query_test.rb`: Added `test_sort_by_parent_id_asc_should_apply_asc_direction`
— verifies `lft ASC` is used for ASC
- `test/unit/project_query_test.rb`: Added `test_sort_by_parent_id_desc_should_apply_desc_direction`
— verifies `lft DESC` is used for DESC (this failed before the fix when direction was hardcoded to ASC)

Actions

Also available in: Atom PDF