第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

一種在 Go 中對結(jié)構(gòu)切片進行排序的靈活而優(yōu)雅的方法

一種在 Go 中對結(jié)構(gòu)切片進行排序的靈活而優(yōu)雅的方法

Go
一只斗牛犬 2023-04-04 17:23:07
假設(shè)我們有一個相當(dāng)復(fù)雜struct的領(lǐng)域,我需要根據(jù)不同的標(biāo)準(zhǔn)在幾個地方進行排序,例如type MySuperType struct {    x0, x1, x2, x3 xType    // possibly even more fields}// sort 1: ascending x0, then descending x1, then more stuff// sort 2: if x4==0 then applyCriteria2a else applyCriteria2bfunc f1(mySuperSlice []MySuperType) {    // sort 'myList' according sort #1    // do something with the sorted list}func f2(mySuperSlice []MySuperType) {    // sort 'myList' according sort #2    // do something with the sorted list}func f3(mySuperSlice []MySuperType) {    // sort 'myList' according sort #1, note: we use sort #1 again!    // do something with the sorted list}建議的解決方案 1:創(chuàng)建一個新類型(別名[]MySuperType)來實現(xiàn)sort.Interface所需的每個排序標(biāo)準(zhǔn)。問題:(i)有一些重復(fù)的代碼,因為函數(shù)Len和Swap將是相同的(ii)將會有一堆新的類型,它們對程序的整體可讀性沒有幫助——這些新類型并不真正代表任何東西,而且唯一真正重要的是函數(shù)Less。建議的解決方案 2:使用sort.Slice這將是完美的解決方案(請參閱此答案),但據(jù)我了解,必須內(nèi)聯(lián)指定排序功能(invalid receiver type []T ([]T is an unnamed type)當(dāng)我嘗試在其他地方定義它時出現(xiàn)錯誤,這意味著我需要定義的別名[]T,我們回到解決方案 1)?,F(xiàn)在,定義內(nèi)聯(lián)函數(shù)的問題是(i)考慮到 的復(fù)雜性MySuperType,函數(shù)可能會很長,并且(ii)函數(shù)將在幾個地方重復(fù)(例如f1在f3我上面的例子中)——比解決方案 1 更煩人的是排序函數(shù)可能又長又復(fù)雜。注意:(i) 如果我們實際上沒有 (ii) 就不會是那么大的問題問題:鑒于我目前對 Go 的理解和知識,我會使用解決方案 1。但是有沒有人知道可以優(yōu)雅地解決這個問題的不同方法或改進上面列出的缺點的建議?
查看完整描述

3 回答

?
慕村9548890

TA貢獻1884條經(jīng)驗 獲得超4個贊

另一種選擇是編寫函數(shù),返回與參數(shù)簽名匹配的切片上的閉包:lesssort.Slice


func ascX0DescX1(s []MySuperType) (func(int, int) bool) {

    return func(i, j int) bool {

        if s[i].x0 == s[j].x0 {

            return s[i].x1 > s[j].x1

        }

        return s[i].x0 < s[j].x0

    }

}

然后將其作為lessarg傳遞給sort.Slice:


sort.Slice(mySuperSlice, ascX0DescX1(mySuperSlice))


查看完整回答
反對 回復(fù) 2023-04-04
?
慕斯709654

TA貢獻1840條經(jīng)驗 獲得超5個贊

為每個排序編寫一個排序函數(shù),并根據(jù)需要從 f1、f2 和 f3 調(diào)用:


func sortByX0AscX1Desc(s []MySuperType) {

    sort.Slice(s, func(i, j int) bool {

        switch {

        case s[i].x0 < s[j].x0:

            return true

        case s[i].x0 > s[j].x0:

            return false

        case s[i].x1 > s[j].x1:

            return true

        default:

            return false

        }

    })

}


func f1(mySuperSlice []MySuperType) {

    sortByX0AscX1Desc(mySuperSlice)

    // do something with the sorted list

}

func f2(mySuperSlice []MySuperType) {

    sortBySomethingElse(mySuperSlice)

    // do something with the sorted list

}

func f3(mySuperSlice []MySuperType) {

    sortByX0AscX1Desc(mySuperSlice)

    // do something with the sorted list

}


查看完整回答
反對 回復(fù) 2023-04-04
?
慕運維8079593

TA貢獻1876條經(jīng)驗 獲得超5個贊

您也可以省略額外的功能并在需要的地方調(diào)用排序。


type MySuperType struct {

    x0, x1, x2, x3 string

}


func f1() {

    fields := []MySuperType {

        { "a1", "b4", "c3", "d2" },

        { "a2", "b1", "c2", "d3" },

        { "a3", "b1", "c4", "d1" },

        { "a4", "b3", "c1", "d4" },

    }

    sort.SliceStable(fields, func(i, j int) bool {

        return fields[i].x1 < fields[j].x1 || fields[i].x2 > fields[j].x2

    })

    fmt.Println("by x1, then x2: ", fields)

}

結(jié)果:通過 x1,然后 x2:[{a3 b1 c4 d1} {a2 b1 c2 d3} {a4 b3 c1 d4} {a1 b4 c3 d2}]


查看完整回答
反對 回復(fù) 2023-04-04
  • 3 回答
  • 0 關(guān)注
  • 198 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號