github.com/anchore/syft@v1.38.2/internal/relationship/sort.go (about) 1 package relationship 2 3 import ( 4 "sort" 5 6 "github.com/anchore/syft/syft/artifact" 7 "github.com/anchore/syft/syft/pkg" 8 ) 9 10 // Sort takes a set of package-to-package relationships and sorts them in a stable order by name and version. 11 // Note: this does not consider package-to-other, other-to-package, or other-to-other relationships. 12 // TODO: ideally this should be replaced with a more type-agnostic sort function that resides in the artifact package. 13 func Sort(rels []artifact.Relationship) { 14 sort.SliceStable(rels, func(i, j int) bool { 15 return less(rels[i], rels[j]) 16 }) 17 } 18 19 func less(i, j artifact.Relationship) bool { 20 iFrom, ok1 := i.From.(pkg.Package) 21 iTo, ok2 := i.To.(pkg.Package) 22 jFrom, ok3 := j.From.(pkg.Package) 23 jTo, ok4 := j.To.(pkg.Package) 24 25 if !ok1 && !ok2 && !ok3 && !ok4 { 26 return false 27 } 28 29 if iFrom.Name != jFrom.Name { 30 return iFrom.Name < jFrom.Name 31 } 32 if iFrom.Version != jFrom.Version { 33 return iFrom.Version < jFrom.Version 34 } 35 if iTo.Name != jTo.Name { 36 return iTo.Name < jTo.Name 37 } 38 if iTo.Version != jTo.Version { 39 return iTo.Version < jTo.Version 40 } 41 return i.Type < j.Type 42 }