Skip to content

Commit 1ad700f

Browse files
Copilotsilverwind
andcommitted
Fix code search sort order by sorting results in memory
- Sort search results by filename in PerformSearch function - Applied to both indexed search (bleve/elasticsearch) and gitgrep fallback - Uses SortStableFunc to preserve existing relevance-based ordering - Ensures stable, alphabetical sort order for results with same relevance score Co-authored-by: silverwind <115237+silverwind@users.noreply.github.com>
1 parent b62f486 commit 1ad700f

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

modules/indexer/code/search.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"bytes"
88
"context"
99
"html/template"
10+
"slices"
1011
"strings"
1112

1213
"code.gitea.io/gitea/modules/highlight"
@@ -139,6 +140,13 @@ func PerformSearch(ctx context.Context, opts *SearchOptions) (int, []*Result, []
139140
return 0, nil, nil, err
140141
}
141142

143+
// Sort results by filename for stable ordering
144+
// The indexer returns results sorted by relevance score, but results with the same score
145+
// should be in a consistent order. Sorting by filename ensures this.
146+
slices.SortStableFunc(results, func(a, b *internal.SearchResult) int {
147+
return strings.Compare(a.Filename, b.Filename)
148+
})
149+
142150
displayResults := make([]*Result, len(results))
143151

144152
for i, result := range results {

0 commit comments

Comments
 (0)