Please make Taxonomy.ByCount produce a stable ordering

The TL;DR:

Please make Taxonomy.ByCount sort first by count, then by name, so as to produce a stable ordering.

The long form:

I have placed hugo’s destination directory in a github repository so that each time I make changes to the source files I can see their effect on the destination files.

For this to work well, it is important that there be no spurious changes in the destination files. By “no spurious changes” I mean that two successive hugo builds on an identical set of source files should produce identical sets of destination files.

As things stand right now, there are some sources of spurious changes that I am struggling with, for example some unnecessary updates to some modification dates, and the Pager 2 weirdness. But these are another story.

One source of spurious changes that I suppose could easily be fixed is the way the Taxonomy.ByCount method works.

As it stands, this method works as intended, but here is the problem:

If I remove a tag, and then add that tag back, then within a group of tags with the same count, some tags may be re-ordered. In other words, the method does not produce a stable ordering.

I suppose that this is happening because the method just sorts on count, but count is not unique, so sorting on count alone gives an unstable ordering.

To fix this, the method should perform sorting using a complete set of keys.

A complete set of keys would be, I suppose, the count followed by the name. This way, items that have the same count would be sorted alphabetically within that count, thus producing a stable ordering. (I suppose two items better not have the same name, or if they do, then it is their fault, not hugo’s fault.)

1 Like

Good idea. Please create a proposal here.

The ByCount method already falls back to sorting alphabetically.

I’ve updated the documentation:
https://gohugo.io/methods/taxonomy/bycount/

Here’s an example that demonstrates the stable sort:

git clone --single-branch -b hugo-github-issue-14095 https://github.com/jmooring/hugo-testing hugo-github-issue-14095
cd hugo-github-issue-14095
hugo server

I suspect that what you are seeing is related making changes while running hugo server.
Do you clear your local public directory before the “build your site + push public directory to repo” stage?

1 Like

Okay then, my troubleshooting of the original problem must have been wrong.
Back to the drawing board.

Thanks for looking into it, and I am sorry I wasted your time.

Well, I used to supply the command-line option that causes hugo to clear it,

but then I removed the option because I decided to clear it myself, because hugo deletes my .gitignore file, (and you guys here don’t seem to think that this is a problem,)

but now I realize I have a problem with hugo not clearing it between builds,

so maybe I should revert that.

Can you explain where that file is in relation to the root of the public directory?

And I presume you are referring to the --cleanDestinationDir flag?

Yes, I am referring to the --cleanDestinationDir flag.

My .gitignore file is at the root of the destination directory, just as my .git directory is.

Hugo is clever enough to refrain from deleting my .git directory, but it likes to delete my .gitignore file. There is another thread around here where this was discussed and from what I understand the resolution was that this is not perceived to be a problem.

Seems like a simple change.

1 Like

Please create a proposal for this. I suggest we limit the change to .gitignore and .gitattributes files.

Done: Please make --cleanDestinationDir spare more than just the .git directory · Issue #14097 · gohugoio/hugo · GitHub