Skip to content

Commit

Permalink
improve sorting
Browse files Browse the repository at this point in the history
  • Loading branch information
jxsl13 committed Jul 17, 2024
1 parent 4ef7787 commit 7f1ffc3
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions sort.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,13 @@ type byMostFilePathSeparators []string
func (a byMostFilePathSeparators) Len() int { return len(a) }
func (a byMostFilePathSeparators) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a byMostFilePathSeparators) Less(i, j int) bool {
ca := strings.Count(a[i], separator)
cb := strings.Count(a[j], separator)

return strings.Count(a[i], separator) > strings.Count(a[j], separator)
if ca == cb {
return a[i] < a[j]
}
return ca > cb
}

// byLeastFilePathSeparators sorts the string by the number of file path separators
Expand All @@ -27,6 +32,11 @@ type byLeastFilePathSeparators []string
func (a byLeastFilePathSeparators) Len() int { return len(a) }
func (a byLeastFilePathSeparators) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a byLeastFilePathSeparators) Less(i, j int) bool {
ca := strings.Count(a[i], separator)
cb := strings.Count(a[j], separator)

return strings.Count(a[i], separator) < strings.Count(a[j], separator)
if ca == cb {
return a[i] < a[j]
}
return ca < cb
}

0 comments on commit 7f1ffc3

Please sign in to comment.