Repository navigation
refactor: replace custom sort types with slices.SortFunc - #9
Closed
ryans-posthog wants to merge 1 commit into
Closed
ryans-posthog wants to merge 1 commit into
ryans-posthog wants to merge 1 commit into
Conversation
Replace four custom sort types (byName, byNameDirFirst, bySize, byTime) and their Len/Swap/Less boilerplate with slices.SortFunc calls. Remove the "sort" import since "slices" was already in use. Generated-By: PostHog Code Task-Id: 8eab9f21-ca28-4327-993f-25dfd950583c
ryans-posthog
commented
Apr 16, 2026
ryans-posthog
left a comment
Owner
Author
There was a problem hiding this comment.
Check up on all the failures and look at my comments.
Comment on lines
+229
to
272
| case sortByNameDirFirst: | ||
| cmp = func(a, b fileInfo) int { | ||
| if a.IsDir != b.IsDir { | ||
| if a.IsDir { | ||
| return -1 | ||
| } | ||
| return 1 | ||
| } | ||
| return strings.Compare(strings.ToLower(a.Name), strings.ToLower(b.Name)) | ||
| } | ||
| case sortBySize: | ||
| cmp = func(a, b fileInfo) int { | ||
| const directoryOffset = -1 << 31 | ||
| aSize, bSize := a.Size, b.Size | ||
| if a.IsDir { | ||
| aSize = directoryOffset | ||
| } | ||
| if b.IsDir { | ||
| bSize = directoryOffset | ||
| } | ||
| if a.IsDir && b.IsDir { | ||
| return strings.Compare(strings.ToLower(a.Name), strings.ToLower(b.Name)) | ||
| } | ||
| if aSize < bSize { | ||
| return -1 | ||
| } | ||
| if aSize > bSize { | ||
| return 1 | ||
| } | ||
| return 0 | ||
| } | ||
| case sortByTime: | ||
| cmp = func(a, b fileInfo) int { | ||
| return a.ModTime.Compare(b.ModTime) | ||
| } | ||
| } | ||
| if cmp != nil { | ||
| if l.Order == "desc" { | ||
| slices.SortFunc(l.Items, func(a, b fileInfo) int { return cmp(b, a) }) | ||
| } else { | ||
| slices.SortFunc(l.Items, cmp) | ||
| } | ||
| } | ||
|
|
Owner
Author
There was a problem hiding this comment.
This code is significantly more complex and harder to read than the original code. It's reduced the LoC but increased the complexity of understanding the code itself.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
byName,byNameDirFirst,bySize,byTime) and theirLen/Swap/Lessboilerplate withslices.SortFunccalls"sort"import sincesliceswas already in useTest plan
go build ./modules/caddyhttp/fileserver/...compiles cleanlygo test ./modules/caddyhttp/fileserver/...passesgo test ./caddytest/...integration tests passGenerated-By: PostHog Code
Task-Id: 8eab9f21-ca28-4327-993f-25dfd950583c