How do you do case-insensitive list sorting

I’ve got a few post titles that start with a lower case letter and others that have an upper case letter. I want to sort .Title in the correct order regardless of case.

When I do range site.RegularPages the sorting is messed up because of the different cases, like so Ab, Ba, ab. I want it to be ab, Ab, Ba

How you handle this? I found a previous discussion about this here. But I don’t follow the implementations and looking to see if there is another workaround since that thread is from last year.


{{ range sort .Pages.Reverse ".Params.Title" }}

Will get you:

Ab ab Ba ba

Which is closer to what you want, but not all the way there.

Tried your your code, but it gives: ba, ab, Ba, Ab

Is there a way to use lower, like below as this would solve the problem.

{{ range sort .Pages (lower ".Params.Title") }}

Using lower gives an error message: <sort .Pages (lower "...>: error calling sort: params is an unexported field of struct type *hugolib.Page

Did you try it exactly as-is? Because I’m not seeing that.

Edit: When testing this, I named my content files:,,, and Then gave them the titles you mentioned. So not sure if this is making a difference.

Copied your code directly, and even just renamed my content files to match what you had done. Still get: `ba, ab, Ba, Ab.

My thinking is that in order to sort the content files correctly, all titles need to be converted to lower case, and then somehow range through those, but output the original titles.:thinking:

@nobby After a bit of poking around, I may have something for you. Give this a shot:

{{ $list := slice }}

{{ range .Pages }}
  {{ $titleLower := lower .Title }}
  {{ $list = $list | append (dict "titleLower" $titleLower "page" .) }}
{{ end }}

{{ range sort $list ".titleLower" }}
  {{ .page.Title }}
{{ end }}

For me, it outputs:


@zwbetz This works great. :clap: