Order of sort, if value is not set in frontmatter?

Hi everyone! Came to the project with couple of content md files that has some props in frontmatter section, like this:

title: ...
icon: ...
category: study
cardTitle: ...

what’s important there are no date or PublishDate fields.
But latter in the html there is such code that loopes through abovementioned md files basing on ByPublishDate and renders these cards on the page.

{{ $pages := .Pages.ByPublishDate.Reverse }}
    <div class="cardClass">
        {{ range where $pages "Params.category" "study" }}

So when i add a new md file it take place not in order I would expect (it takes a place in the middle, not in the end or at the beggining of cards positioning). While experimenting I found out, that the field being used for sort - is title. I studied Lists of Content in Hugo | Hugo page, but it doesn’t cover my case.

1 ) What is the default sort field from frontmatter, if value mentioned in range functionality is not present?
2) Could somebody please explain such behaviour?
3) The best option to fix such an unpredicted behaviour would be to add date attribute to frontmatter part of md files, right?

  1. For ByPublishDate there is no fall back, so the sort you see is what you get when sorting the letters “aaaaaaaaaaaaaaaaa”.
  2. See above.
  3. I would recommend using the default sort in Hugo; this sorts by Weight (from front matter, if set), .Date or .LinkTitle.

In general I would recommend that you always have at least one date in your posts, as a lot of things in Hugo is built around it.

1 Like

Thank you very much! That was really helpful!

Depending on the number of articles you have, if you don’t want to have dates at all, then weight is the best option. Hugo prioritizes weight from negative to positive, so you can choose the sequence you want for your weights, bearing in mind the future and how often you post. I have a section in my site without dates and I opted for weight in the files themselves from 1000 going up for existing files and any new contents get the weight in reverse order, i.e 999, 998 etc. Then you can sort by weight in your templates. I am not sure how you can tell the sequence of the posts from the oldest to the current one, but if you can, then setting weight: xx in the front matter is the way to go.

Though as Bep says, using dates is better and that does not mean you have to display them in your content.

1 Like

I can add that:

  1. LinktTitle tries both linkTitle and title in front matter
  2. There is a date configuration that can be useful: Configure Hugo | Hugo
  3. Also see the cascade documentation for setting default values.