gohugoio/hugo · error
can't sort {type}
Error message
can't sort {type} What it means
The `sort` function only handles arrays, slices, and maps. After the kind switch at sort.go:44, any other reflect.Kind (string, int, struct, bool, etc.) falls into the default branch and produces this error with the type name interpolated. Sorting a scalar or a single struct is not meaningful.
Source
Thrown at tpl/collections/sort.go:50
if l == nil {
return nil, errors.New("sequence must be provided")
}
seqv, isNil := hreflect.Indirect(reflect.ValueOf(l))
if isNil {
return nil, errors.New("can't iterate over a nil value")
}
ctxv := reflect.ValueOf(ctx)
var sliceType reflect.Type
switch seqv.Kind() {
case reflect.Array, reflect.Slice:
sliceType = seqv.Type()
case reflect.Map:
sliceType = reflect.SliceOf(seqv.Type().Elem())
default:
return nil, errors.New("can't sort " + reflect.ValueOf(l).Type().String())
}
collator := langs.GetCollator1(ns.deps.Conf.Language().(*langs.Language))
// Create a list of pairs that will be used to do the sort
p := pairList{Collator: collator, sortComp: ns.sortComp, SortAsc: true, SliceType: sliceType}
p.Pairs = make([]pair, seqv.Len())
var sortByField string
for i, l := range args {
dStr, err := cast.ToStringE(l)
switch {
case i == 0 && err != nil:
sortByField = ""
case i == 0 && err == nil:
sortByField = dStr
case i == 1 && err == nil && dStr == "desc":
p.SortAsc = falseView on GitHub (pinned to 52c9bd7908)
Solutions
- Pass a slice or array: `{{ sort .Pages }}`.
- To sort by a field, pass the collection as the first arg and the field name as the second: `{{ sort .Pages "Title" }}`.
- Verify the variable is a collection before calling sort.
Example fix
// before
{{ sort .Title }}
// after
{{ sort .Pages "Title" }} Defensive patterns
Strategy: type-guard
Type guard
func isSortable(v any) bool {
if v == nil { return false }
switch reflect.TypeOf(v).Kind() {
case reflect.Slice, reflect.Array, reflect.Map: return true
default: return false
}
} Prevention
- Pass only slices, arrays, or maps to sort.
- Use the two-arg form `sort $collection "field"` to sort by a field.
- Double-check you are passing the collection, not a single element.
When it happens
Trigger: Calling sort on a non-iterable value: `{{ sort "hello" }}`, `{{ sort 42 }}`, `{{ sort .Title }}` (a string), or `{{ sort .Page }}` (a struct).
Common situations: A developer accidentally passes a single page object or a scalar field instead of a collection, e.g. `{{ sort .Pages.Title }}` (a string) rather than `{{ sort .Pages "Title" }}` (sort the slice by field).
Related errors
- sequence must be provided
- can't iterate over a nil value
- operator argument must be string type
- isset unable to use key of type %T as index
- type %T not supported
AI-assisted analysis of gohugoio/hugo@52c9bd7908 (2026-08-09).
Data as JSON: /api/errors/9ddccbc8615278c7.
Report an issue: GitHub.