github.com/anchore/syft@v1.4.2-0.20240516191711-1bec1fc5d397/syft/cpe/by_source_then_specificity.go (about)

     1  package cpe
     2  
     3  import "sort"
     4  
     5  type BySourceThenSpecificity []CPE
     6  
     7  func (b BySourceThenSpecificity) Len() int {
     8  	return len(b)
     9  }
    10  
    11  func (b BySourceThenSpecificity) Less(i, j int) bool {
    12  	sourceOrder := map[Source]int{
    13  		NVDDictionaryLookupSource: 1,
    14  		DeclaredSource:            2,
    15  		GeneratedSource:           3,
    16  	}
    17  
    18  	getRank := func(source Source) int {
    19  		if rank, exists := sourceOrder[source]; exists {
    20  			return rank
    21  		}
    22  		return 4 // Sourced we don't know about can't be assigned special priority, so
    23  		// are considered ties.
    24  	}
    25  	iSource := b[i].Source
    26  	jSource := b[j].Source
    27  	rankI, rankJ := getRank(iSource), getRank(jSource)
    28  	if rankI != rankJ {
    29  		return rankI < rankJ
    30  	}
    31  
    32  	return isMoreSpecific(b[i].Attributes, b[j].Attributes)
    33  }
    34  
    35  func (b BySourceThenSpecificity) Swap(i, j int) {
    36  	b[i], b[j] = b[j], b[i]
    37  }
    38  
    39  var _ sort.Interface = (*BySourceThenSpecificity)(nil)