From 398d9a6bb5bb56eb7a23101bc8cbebd7181c90af Mon Sep 17 00:00:00 2001 From: W-Mark Kubacki Date: Wed, 29 Mar 2017 16:40:29 +0200 Subject: [PATCH] browse: when sorting by size, sort directory section by name Previously directories have been merely pulled to the front, and then sorted arbitrarily. That is, their order among themselves depended on the filesystem implementations. Something opaque to the visitor. This fixes said inconsistency, and implements the by-size-then-by-name order I initially intended for this. --- caddyhttp/browse/browse.go | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/caddyhttp/browse/browse.go b/caddyhttp/browse/browse.go index 9a7e8e1c6..ab1e4d12e 100644 --- a/caddyhttp/browse/browse.go +++ b/caddyhttp/browse/browse.go @@ -154,12 +154,21 @@ func (l bySize) Swap(i, j int) { l.Items[i], l.Items[j] = l.Items[j], l.Items[i] const directoryOffset = -1 << 31 // = math.MinInt32 func (l bySize) Less(i, j int) bool { iSize, jSize := l.Items[i].Size, l.Items[j].Size + + // Directory sizes depend on the filesystem implementation, + // which is opaque to a visitor, and should indeed does not change if the operator choses to change the fs. + // For a consistent user experience directories are pulled to the front… if l.Items[i].IsDir { - iSize = directoryOffset + iSize + iSize = directoryOffset } if l.Items[j].IsDir { - jSize = directoryOffset + jSize + jSize = directoryOffset } + // … and sorted by name. + if l.Items[i].IsDir && l.Items[j].IsDir { + return strings.ToLower(l.Items[i].Name) < strings.ToLower(l.Items[j].Name) + } + return iSize < jSize }