Skip to content

Commit

Permalink
trim volume on windows for sorting
Browse files Browse the repository at this point in the history
  • Loading branch information
jxsl13 committed Jul 17, 2024
1 parent 86aeb56 commit ce98def
Showing 1 changed file with 17 additions and 8 deletions.
25 changes: 17 additions & 8 deletions sort.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@ 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)
ai := TrimVolume(a[i])
aj := TrimVolume(a[j])

ca := strings.Count(ai, separator)
cb := strings.Count(aj, separator)

/*
Edge case where the root path is compared to a file in the root path.
Expand All @@ -27,17 +30,19 @@ func (a ByMostFilePathSeparators) Less(i, j int) bool {
*/

// root = smallest number of separators
if ca == 1 && a[i] == separator {
if ca == 1 && ai == separator {
ca = -1
}

if cb == 1 && a[j] == separator {
if cb == 1 && aj == separator {
cb = -1
}

if ca == cb {
// with volume
return a[i] < a[j]
}

return ca > cb
}

Expand All @@ -49,8 +54,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)
ai := TrimVolume(a[i])
aj := TrimVolume(a[j])

ca := strings.Count(ai, separator)
cb := strings.Count(aj, separator)

/*
Edge case where the root path is compared to a file in the root path.
Expand All @@ -61,15 +69,16 @@ func (a ByLeastFilePathSeparators) Less(i, j int) bool {
*/

// root = smallest number of separators
if ca == 1 && a[i] == separator {
if ca == 1 && ai == separator {
ca = -1
}

if cb == 1 && a[j] == separator {
if cb == 1 && aj == separator {
cb = -1
}

if ca == cb {
// with volume
return a[i] < a[j]
}
return ca < cb
Expand Down

0 comments on commit ce98def

Please sign in to comment.