Sorting by boolean, possible?

Hi Hugoers,

I’m building a page that serves a list of speakers for a conference we’re putting on, and one of the params in the front matter is featured, which has a value of either true or false.


name = "Frank Spellman"
bio = ""
id = 697227
title = "VP of Engineering"
featured = true
organization = "ThingCorp"
sessions = []
name = "headshot"
src = "headshot.jpg"

I’d like to be able to sort so that featured speakers are in the top of my list and the rest of the speakers follow but I have not had any luck getting sort to work for me using this:

{{ range sort $speakers ".Params.featured" }}
{{ end }}

This doesn’t seem to have any effect, but seems to be limited to boolean params as if I sort by name alone using

{{ range sort $speakers "" }}
{{ end }}

then it works as expected.

I’ve searched but I haven’t found anything related (though searching for sort and bool doesn’t exactly narrow down results very well since they’re such core concepts of most development).

Are there any limitations with sorting by bool’s in frontmatter? Would I be better served changing them to integers 0 and 1?


Hello @peck. I would approach this slightly different.

Instead of sorting by that boolean param, have two sets of pages. One where the param is true. One where it’s false.

Then concat the sets in the order you like.

@zwbetz hmm, interesting. I wouldn’t have thought of doing that. Right now all my speakers are living under content/speakers/<speaker-name-slug/ where each speaker had a directory for containing their with frontmatter.

I’m not sure I understand the benefits of splitting into sets here. Could you help me understand?

I’m currently using the featured boolean approach successfully on our front page where I’m only showing the featured speakers using

{{ $speakers := where (where .Site.RegularPages "Section" "speakers") ".Params.featured" "=" true }}

So on second thought, scratch that suggestion.

Trying ordering them by param instead.


{{ $speakers := where site.RegularPages "Section" "speakers" }}

Then try one of these:

{{ range ($speakers.ByParam "featured") }}
{{ range ($speakers.ByParam "featured").Reverse }}
1 Like

@zwbetz that worked perfectly. Thank you! I’m still confused as to why the sort wasn’t working the way I expected but I’m very thankful for the quick solution!

Good deal :+1:

And I’m not sure either on that one. Maybe others can chime in.