Skip to content

Commit 5ed652c

Browse files
authored
Merge pull request #31 from scanoss/bugfix/mdaloia/SP-3557-SCANOSS-HFH-Sort-response-components-by-ascending-order
[SP-3557] fix: sort results by ascending order
2 parents 6134797 + b1a560a commit 5ed652c

File tree

3 files changed

+584
-3
lines changed

3 files changed

+584
-3
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
## [0.7.1] - 2025-10-28
9+
### Fixed
10+
- Fix sorting order of results. Now sorting by `Order` field in ascending order, instead of `PathId`.
11+
812
## [0.7.0] - 2025-10-24
913
## Changed
1014
- Update qdrant's docker compose file name
@@ -46,3 +50,4 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
4650
[0.5.0]: https://github.com/scanoss/folder-hashing-api/compare/v0.4.2...v0.5.0
4751
[0.6.0]: https://github.com/scanoss/folder-hashing-api/compare/v0.5.0...v0.6.0
4852
[0.7.0]: https://github.com/scanoss/folder-hashing-api/compare/v0.6.0...v0.7.0
53+
[0.7.1]: https://github.com/scanoss/folder-hashing-api/compare/v0.7.0...v0.7.1

internal/service/scan_service_impl.go

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package service
1818

1919
import (
2020
"context"
21+
"math"
2122
"sort"
2223

2324
"github.com/grpc-ecosystem/go-grpc-middleware/logging/zap/ctxzap"
@@ -221,9 +222,23 @@ func (s *ScanServiceImpl) deduplicateComponents(results []*entities.ScanResult)
221222
}
222223
}
223224

224-
sort.Slice(deduplicatedResults, func(i, j int) bool {
225-
return deduplicatedResults[i].PathID < deduplicatedResults[j].PathID
226-
})
225+
sortByBestComponentOrder(deduplicatedResults)
227226

228227
return deduplicatedResults
229228
}
229+
230+
// sortByBestComponentOrder sorts the results by the lowest order of the component groups (lower order is better).
231+
func sortByBestComponentOrder(results []*entities.ScanResult) {
232+
sort.SliceStable(results, func(i, j int) bool {
233+
minOrderI := int32(math.MaxInt32)
234+
for _, group := range results[i].ComponentGroups {
235+
minOrderI = min(minOrderI, group.Order)
236+
}
237+
238+
minOrderJ := int32(math.MaxInt32)
239+
for _, group := range results[j].ComponentGroups {
240+
minOrderJ = min(minOrderJ, group.Order)
241+
}
242+
return minOrderI < minOrderJ
243+
})
244+
}

0 commit comments

Comments
 (0)