Future Impact About Negative Weight Value?

Glad to play with Hugo. I don’t use any date or published date for some reasons. So, for few projects I only use weight value to order sections inside content folder.

The problem about ordering by weight is when we start from weight: 1 and the content grow huge (about hundred or thousand files) then someday we need to add new priority items that need to be placed about weight: 1 to weight: 10.

The solution is simple when content isn’t huge. Maybe we can change the weight one by one. But if content is huge and complex, even changing order (from .ByWeight to .ByWeight.Reverse) can mess another structure.

My current trick is just add new priority items with negative weight value such weight: -1 or weight: -2 etc. So, we only need to change few files to fit the order by weight, not to change hundred or thousand weight files one by one.

But I don’t know if my trick above is good for future due to upgrading version or another issue. Or maybe any other trick about it?

in the Docs is nothing about value type and range. Hugo barks if you dont use a number.
@bep please extend the doc

1 Like

Start weighing at 10 and increase by 10. Then if you later need to put something to the top, give it a weight of e.g. 5. Or start at 100 and increase by 100 for even more insanity flexibility :wink:.

1 Like

Thanks for reply. I use latest Hugo version on Netlify and there’s no error or warning about negative weight value. But I don’t know about next version.

Thanks for the tips. Yes, it’s work for starting Hugo site before the content grow with unflexible weight scaling value. I think your tips (about scaling number by 10 or more) is important to be official documented.

in the old “punch card” time we used numbers in increments of 10 or 100. There was (not) always a gap to insert something :wink:

1 Like

I know that ordered by weight is about priority, which smaller number is more priority than bigger number. I think it’s work well for prioritize menu. But when it come to posts or sections (if don’t use date for ordering files), then older posts will always come as top post due to smaller number value. Because on my first assumption, the first post (that filled with smaller number) will always come as older post on the bottom page, not on the top page. This is the reason why I use negative weight value to make newest posts always come on the top page. By the way, is it safe using negative weight value for long term usage?

So, then why use weight? you have an ordered list (by date) by default. Reversing the list gives you the order you desire…

In general, asking “is it safe using ______ for long term usage?” is an rabbit hole and “part of the job”.

That being said, do i think that this behavior is sacrosanct in Hugo? probably less so than being Hugo being written in Go… do I think that it is a behavior which just makes computational sense for how sorting works? yeah.

1 Like

Thanks for reply. I don’t use any date and time format (on my content, layouts, front matter, etc) for some reasons. I don’t want any date appear on my website and search engine result. Also, I ordered one list.html layouts for few different sections with different ordered format. Example, for blog section I need ordered by newest first, but for poem section I need ordered by alphabetical first. And I think, ordered by weight can be used for all different section purpose. Alternatively, maybe I can use different layout list for each different section to avoid adding weight: bla bla bla on each content file.

You can still use front matter date to order your pages, but then just don’t display it in your rendered site.


define the date usage in the config (like this)

date                       = [ "date", ":filename", ":default"]
publishDate                = [ "publishDate", "date"]
lastmod                    = [ "lastmod", ":fileModTime", "publishDate"]

If you haven’t a date defined, hugo uses the file creation date, very easy.
For *hugo new you can define the date values in the archetype file

categories  = ["Post"]
tags        = ["{{ now.Format "2006"}}"]
description = "{{ replace .TranslationBaseName "-" " " | title }}"
title       = "{{ replace .TranslationBaseName "-" " " | title }}"
date        = "{{ (.Date |time).Format "2006-01-02T15:04:05Z07:00" }}"
draft       = false
1 Like

Thanks. I will try it.