Skip to content

Commit

Permalink
remove reflection-based slice sort
Browse files Browse the repository at this point in the history
Signed-off-by: Miguel Molina <miguel@erizocosmi.co>
  • Loading branch information
erizocosmico committed Jun 26, 2017
1 parent 804bf53 commit c28f6fa
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion classifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func (c *classifier) Classify(content []byte, candidates map[string]float64) []s
}

func sortLanguagesByScore(scoredLangs []*scoredLanguage) []string {
sort.SliceStable(scoredLangs, func(i, j int) bool { return scoredLangs[j].score < scoredLangs[i].score })
sort.Stable(byScore(scoredLangs))
sortedLanguages := make([]string, 0, len(scoredLangs))
for _, scoredLang := range scoredLangs {
sortedLanguages = append(sortedLanguages, scoredLang.language)
Expand Down Expand Up @@ -94,3 +94,9 @@ func (c *classifier) tokenProbability(token, language string) float64 {

return tokenProb
}

type byScore []*scoredLanguage

func (b byScore) Len() int { return len(b) }
func (b byScore) Swap(i, j int) { b[i], b[j] = b[j], b[i] }
func (b byScore) Less(i, j int) bool { return b[j].score < b[i].score }

0 comments on commit c28f6fa

Please sign in to comment.