gohugoio / hugo

The world’s fastest framework for building websites.
https://gohugo.io
Apache License 2.0
75.37k stars 7.49k forks source link

Add support for multiple keys in sort #11588

Open bep opened 12 months ago

bep commented 12 months ago

E.g.

{{ sort site.Params.grades "value1" "asc" "value2" "desc" }} 

But we also need a way to handle zero values, as demonstrated by Hugo's default page sort:

This would not work:

{{ sort site.Params.grades "weight" "asc" "lastMod" "desc" }} 

Maybe something ala:

{{ sort site.Params.grades 
  dict ( "key" "weight" "order" "asc" "zeroIsNotAValue" true }}
 }} 

Where zeroIsNotAValue need to be replaced with a better term, but it would effectively work as in the less func below:

DefaultPageSort = func(p1, p2 Page) bool {
        o1, o2 := getOrdinals(p1, p2)
        if o1 != o2 && o1 != -1 && o2 != -1 {
            return o1 < o2
        }
        if p1.Weight() == p2.Weight() {
            if p1.Date().Unix() == p2.Date().Unix() {
                c := collatorStringCompare(func(p Page) string { return p.LinkTitle() }, p1, p2)
                if c == 0 {
                    if p1.File().IsZero() || p2.File().IsZero() {
                        return p1.File().IsZero()
                    }
                    return compare.LessStrings(p1.File().Filename(), p2.File().Filename())
                }
                return c < 0
            }
            return p1.Date().Unix() > p2.Date().Unix()
        }

        if p2.Weight() == 0 {
            return true
        }

        if p1.Weight() == 0 {
            return false
        }

        return p1.Weight() < p2.Weight()
    }
u7498708 commented 12 months ago

Can I help at all with implementing this? I am a first time contributor and I think I could probably help implement this. Let me know! :)

bep commented 12 months ago

@u7498708 the "proposal" label needs to come off before we talk implementation.

u7498708 commented 12 months ago

@bep Alrighty then, if there are any other issues that I could possibly help with as a first time contributor let me know!