refactor: replace custom sort types with slices.SortFunc#13
Open
ryans-posthog wants to merge 2 commits intomasterfrom
Open
refactor: replace custom sort types with slices.SortFunc#13ryans-posthog wants to merge 2 commits intomasterfrom
ryans-posthog wants to merge 2 commits intomasterfrom
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: ebf74917-a9d2-4455-b94a-776bbdb1036b
ryans-posthog
commented
Apr 16, 2026
Owner
Author
ryans-posthog
left a comment
There was a problem hiding this comment.
Address the feedback, this fix appears to add more complexity to the visual understanding of the code.
Comment on lines
+225
to
+258
| case sortByName: | ||
| slices.SortFunc(l.Items, func(a, b fileInfo) int { | ||
| return cmp.Compare(strings.ToLower(a.Name), strings.ToLower(b.Name)) | ||
| }) | ||
| case sortByNameDirFirst: | ||
| slices.SortFunc(l.Items, func(a, b fileInfo) int { | ||
| if a.IsDir != b.IsDir { | ||
| if a.IsDir { | ||
| return -1 | ||
| } | ||
| return 1 | ||
| } | ||
| return cmp.Compare(strings.ToLower(a.Name), strings.ToLower(b.Name)) | ||
| }) | ||
| case sortBySize: | ||
| slices.SortFunc(l.Items, 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 cmp.Compare(strings.ToLower(a.Name), strings.ToLower(b.Name)) | ||
| } | ||
| return cmp.Compare(aSize, bSize) | ||
| }) | ||
| case sortByTime: | ||
| slices.SortFunc(l.Items, func(a, b fileInfo) int { | ||
| return a.ModTime.Compare(b.ModTime) | ||
| }) | ||
| } |
Owner
Author
There was a problem hiding this comment.
These new functions are much more unreadable than the previous ones. How many lines were saved if you remove the boilerplate lines of function definitions?
Move sort comparison logic into named package-level functions (compareByName, compareByNameDirFirst, compareBySize, compareByTime) so applySortAndLimit reads cleanly as a simple switch statement. Generated-By: PostHog Code Task-Id: ebf74917-a9d2-4455-b94a-776bbdb1036b
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 pass