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
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

1 participant