mirror of
https://github.com/gohugoio/hugo.git
synced 2025-09-01 22:42:45 +02:00
Make string sorting (e.g. ByTitle, ByLinkTitle and ByParam) language aware
Fixes #2180
This commit is contained in:
@@ -20,6 +20,7 @@ import (
|
||||
"strings"
|
||||
|
||||
"github.com/gohugoio/hugo/common/maps"
|
||||
"github.com/gohugoio/hugo/langs"
|
||||
"github.com/gohugoio/hugo/tpl/compare"
|
||||
"github.com/spf13/cast"
|
||||
)
|
||||
@@ -47,8 +48,10 @@ func (ns *Namespace) Sort(seq any, args ...any) (any, error) {
|
||||
return nil, errors.New("can't sort " + reflect.ValueOf(seq).Type().String())
|
||||
}
|
||||
|
||||
collator := langs.GetCollator(ns.deps.Language)
|
||||
|
||||
// Create a list of pairs that will be used to do the sort
|
||||
p := pairList{SortAsc: true, SliceType: sliceType}
|
||||
p := pairList{Collator: collator, SortAsc: true, SliceType: sliceType}
|
||||
p.Pairs = make([]pair, seqv.Len())
|
||||
|
||||
var sortByField string
|
||||
@@ -124,6 +127,10 @@ func (ns *Namespace) Sort(seq any, args ...any) (any, error) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
collator.Lock()
|
||||
defer collator.Unlock()
|
||||
|
||||
return p.sort(), nil
|
||||
}
|
||||
|
||||
@@ -137,6 +144,7 @@ type pair struct {
|
||||
|
||||
// A slice of pairs that implements sort.Interface to sort by Value.
|
||||
type pairList struct {
|
||||
Collator *langs.Collator
|
||||
Pairs []pair
|
||||
SortAsc bool
|
||||
SliceType reflect.Type
|
||||
@@ -151,16 +159,16 @@ func (p pairList) Less(i, j int) bool {
|
||||
if iv.IsValid() {
|
||||
if jv.IsValid() {
|
||||
// can only call Interface() on valid reflect Values
|
||||
return sortComp.Lt(iv.Interface(), jv.Interface())
|
||||
return sortComp.LtCollate(p.Collator, iv.Interface(), jv.Interface())
|
||||
}
|
||||
|
||||
// if j is invalid, test i against i's zero value
|
||||
return sortComp.Lt(iv.Interface(), reflect.Zero(iv.Type()))
|
||||
return sortComp.LtCollate(p.Collator, iv.Interface(), reflect.Zero(iv.Type()))
|
||||
}
|
||||
|
||||
if jv.IsValid() {
|
||||
// if i is invalid, test j against j's zero value
|
||||
return sortComp.Lt(reflect.Zero(jv.Type()), jv.Interface())
|
||||
return sortComp.LtCollate(p.Collator, reflect.Zero(jv.Type()), jv.Interface())
|
||||
}
|
||||
|
||||
return false
|
||||
|
Reference in New Issue
Block a user