{"record":{"id":"9ddccbc8615278c7","repo":"gohugoio/hugo","slug":"can-t-sort-type","errorCode":null,"errorMessage":"can't sort {type}","messagePattern":"can't sort (.+?)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"tpl/collections/sort.go","lineNumber":50,"sourceCode":"\tif l == nil {\n\t\treturn nil, errors.New(\"sequence must be provided\")\n\t}\n\n\tseqv, isNil := hreflect.Indirect(reflect.ValueOf(l))\n\tif isNil {\n\t\treturn nil, errors.New(\"can't iterate over a nil value\")\n\t}\n\n\tctxv := reflect.ValueOf(ctx)\n\n\tvar sliceType reflect.Type\n\tswitch seqv.Kind() {\n\tcase reflect.Array, reflect.Slice:\n\t\tsliceType = seqv.Type()\n\tcase reflect.Map:\n\t\tsliceType = reflect.SliceOf(seqv.Type().Elem())\n\tdefault:\n\t\treturn nil, errors.New(\"can't sort \" + reflect.ValueOf(l).Type().String())\n\t}\n\n\tcollator := langs.GetCollator1(ns.deps.Conf.Language().(*langs.Language))\n\n\t// Create a list of pairs that will be used to do the sort\n\tp := pairList{Collator: collator, sortComp: ns.sortComp, SortAsc: true, SliceType: sliceType}\n\tp.Pairs = make([]pair, seqv.Len())\n\n\tvar sortByField string\n\tfor i, l := range args {\n\t\tdStr, err := cast.ToStringE(l)\n\t\tswitch {\n\t\tcase i == 0 && err != nil:\n\t\t\tsortByField = \"\"\n\t\tcase i == 0 && err == nil:\n\t\t\tsortByField = dStr\n\t\tcase i == 1 && err == nil && dStr == \"desc\":\n\t\t\tp.SortAsc = false","sourceCodeStart":32,"sourceCodeEnd":68,"githubUrl":"https://github.com/gohugoio/hugo/blob/52c9bd7908b4d02d4d0ff8f82a888834d6ee10d2/tpl/collections/sort.go#L32-L68","documentation":"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.","triggerScenarios":"Calling sort on a non-iterable value: `{{ sort \"hello\" }}`, `{{ sort 42 }}`, `{{ sort .Title }}` (a string), or `{{ sort .Page }}` (a struct).","commonSituations":"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).","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."],"exampleFix":"// before\n{{ sort .Title }}\n// after\n{{ sort .Pages \"Title\" }}","handlingStrategy":"type-guard","validationCode":null,"typeGuard":"func isSortable(v any) bool {\n    if v == nil { return false }\n    switch reflect.TypeOf(v).Kind() {\n    case reflect.Slice, reflect.Array, reflect.Map: return true\n    default: return false\n    }\n}","tryCatchPattern":null,"preventionTips":["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."],"tags":["hugo","template","collections","sort","type-error"],"backgroundTag":null,"analyzedSha":"52c9bd7908b4d02d4d0ff8f82a888834d6ee10d2","analyzedAt":"2026-08-09T21:49:36.660Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}