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

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

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

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

Go
一只斗牛犬 2023-04-04 17:23:07
假設(shè)我們有一個(gè)相當(dāng)復(fù)雜struct的領(lǐng)域,我需要根據(jù)不同的標(biāo)準(zhǔn)在幾個(gè)地方進(jì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)建一個(gè)新類(lèi)型(別名[]MySuperType)來(lái)實(shí)現(xiàn)sort.Interface所需的每個(gè)排序標(biāo)準(zhǔn)。問(wèn)題:(i)有一些重復(fù)的代碼,因?yàn)楹瘮?shù)Len和Swap將是相同的(ii)將會(huì)有一堆新的類(lèi)型,它們對(duì)程序的整體可讀性沒(méi)有幫助——這些新類(lèi)型并不真正代表任何東西,而且唯一真正重要的是函數(shù)Less。建議的解決方案 2:使用sort.Slice這將是完美的解決方案(請(qǐng)參閱此答案),但據(jù)我了解,必須內(nèi)聯(lián)指定排序功能(invalid receiver type []T ([]T is an unnamed type)當(dāng)我嘗試在其他地方定義它時(shí)出現(xiàn)錯(cuò)誤,這意味著我需要定義的別名[]T,我們回到解決方案 1)?,F(xiàn)在,定義內(nèi)聯(lián)函數(shù)的問(wèn)題是(i)考慮到 的復(fù)雜性MySuperType,函數(shù)可能會(huì)很長(zhǎng),并且(ii)函數(shù)將在幾個(gè)地方重復(fù)(例如f1在f3我上面的例子中)——比解決方案 1 更煩人的是排序函數(shù)可能又長(zhǎng)又復(fù)雜。注意:(i) 如果我們實(shí)際上沒(méi)有 (ii) 就不會(huì)是那么大的問(wèn)題問(wèn)題:鑒于我目前對(duì) Go 的理解和知識(shí),我會(huì)使用解決方案 1。但是有沒(méi)有人知道可以?xún)?yōu)雅地解決這個(gè)問(wèn)題的不同方法或改進(jìn)上面列出的缺點(diǎn)的建議?
查看完整描述

3 回答

?
慕村9548890

TA貢獻(xiàn)1884條經(jīng)驗(yàn) 獲得超4個(gè)贊

另一種選擇是編寫(xiě)函數(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))


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

TA貢獻(xiàn)1840條經(jīng)驗(yàn) 獲得超5個(gè)贊

為每個(gè)排序編寫(xiě)一個(gè)排序函數(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

}


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

TA貢獻(xiàn)1876條經(jīng)驗(yàn) 獲得超5個(gè)贊

您也可以省略額外的功能并在需要的地方調(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é)果:通過(guò) x1,然后 x2:[{a3 b1 c4 d1} {a2 b1 c2 d3} {a4 b3 c1 d4} {a1 b4 c3 d2}]


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

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢(xún)優(yōu)惠詳情

幫助反饋 APP下載

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

公眾號(hào)

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