Skip to content

Conversation

@ChandlerSwift
Copy link
Contributor

@ChandlerSwift ChandlerSwift commented Oct 6, 2025

For files with regions of numbers and other characters, it's intuitive to have the numbers sorted by increasing numeric value rather than by ASCII code. (Jeff Atwood has a nice article here:
https://blog.codinghorror.com/sorting-for-humans-natural-sort-order/)

For example, the listing for a directory containing foo1, foo2, and foo10 would sort in that order, rather than putting foo10 between foo1 and foo2.

Closes #7226

Since this library is so little code, I opted to copy the code into the repo rather than adding it as a dependency. (Per the Go Proverb, "A little copying is better than a little dependency" 🙂)

Responding to comments from the issue that this PR closes:

I actually use https://pkg.go.dev/github.com/maruel/natural, which you mentioned above, in another project already, and so far it seems to be correct and efficient.

I tried (twice!) to switch this PR over to use that package instead, but wasn't able to convince myself it was correct. (I have no reason to believe it's not correct either! I just couldn't quite internalize it enough to be able to vouch for its correctness. Not sure why—it seems well written!—but I guess that implementation just doesn't quite mesh with the way I think about the algorithm.)

Because of that, I stuck with the implementation from the https://github.com/fvbommel/sortorder package, for which I can say I agree with its correctness. That said, if you'd rather I swap over to the other implementation, I'm definitely happy to do that.

Maybe only the HTML output should be natural sorted though.

Right now, the code makes the assumption that the output will be the same regardless of the format it is presented in. If I were to special-case the HTML output, I'd have to pass some additional context into applySortAndLimit (and maybe a level further?), too, which would balloon the scope of this change.

I'd personally find it surprising if the sort implementation varied depending on what format the output is returned in, so I'd advocate for natural sort regardless of output format


The included test was drafted by a large language model, but largely rewritten (and validated) by me.

For files with regions of numbers and other characters, it's intuitive
to have the numbers sorted by increasing numeric value rather than by
ASCII code. (Jeff Atwood has a nice article here:
https://blog.codinghorror.com/sorting-for-humans-natural-sort-order/)

For example, the listing for a directory containing `foo1`, `foo2`, and
`foo10` would sort in that order, rather than putting `foo10` between
`foo1` and `foo2`.

Closes caddyserver#7226
@Forza-tng
Copy link
Contributor

I've tested the implementation for my own web/fileserver.

With normal version number style filenames, things look good, but when numbers are real decimal numbers, it doesn't quite work. I think it is "how it should be" in that the algorithm assumes more digits means larger numbers. But in the context of a real decimal number it is not correct.

Version number sort means

  • 1.0.1
  • 1.0.3
  • 1.1.0
  • 1.2.0

Decimal number sort should be:

  • 0.100
  • 0.10001
  • 0.1001
  • 0.101
  • 0.123

Perhaps there ought to be a Caddyfile option for specifying what type of sort that should be applied for specific directory trees?

Good example:

https://mirrors.tnonline.net/btrfs/btrfs-progs/sources/kernel.org/
Screenshot_20251101_194358_Opera beta

Bad example:

https://mirrors.tnonline.net/misc/img-compare/001_Juvenile_leopard_in_the_Serengeti_National_Park_Photo_by_Giles_Laurent/640x427/
Screenshot_20251101_194258_Opera beta

@francislavoie francislavoie changed the title file_server: Implement natural sort for browse templates fileserver: Implement natural sort for browse templates Nov 1, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Natural sorting option for file_server file browse file listings

2 participants