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

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

如何按兩個(gè)值對地圖進(jìn)行排序?

如何按兩個(gè)值對地圖進(jìn)行排序?

Go
千萬里不及你 2023-07-04 17:29:20
我正在嘗試按地圖的兩個(gè)值對地圖進(jìn)行排序。首先是時(shí)間戳,然后是隨機(jī)數(shù)。換句話說,我需要能夠迭代并首先打印具有最小時(shí)間戳的映射,然后是隨機(jī)數(shù)值。像這樣:    "tx1": Transaction{Value:10, Nonce:1, Timestamp:1563543005},    "tx2": Transaction{Value:20, Nonce:2, Timestamp:1563543005},    "tx6": Transaction{Value:60, Nonce:2, Timestamp:1563543005},    "tx5": Transaction{Value:50, Nonce:4, Timestamp:1563543005},    "tx7": Transaction{Value:70, Nonce:1, Timestamp:1563543006},    "tx3": Transaction{Value:30, Nonce:3, Timestamp:1563543006},    "tx4": Transaction{Value:40, Nonce:4, Timestamp:1563543006},這是我的代碼:https://play.golang.org/p/hXo5clCrlU1package mainimport (    "fmt"    "sort")type Transaction struct {    Value         uint64                        `json:"value"`    Nonce         uint64                        `json:"nonce"`    Timestamp     int64                         `json:"timestamp"`}func main() {    // To create a map as input    memPool := map[string]Transaction {        "tx1": Transaction{Value:10, Nonce:1, Timestamp:1563543005},        "tx2": Transaction{Value:20, Nonce:2, Timestamp:1563543005},        "tx3": Transaction{Value:30, Nonce:3, Timestamp:1563543006},        "tx4": Transaction{Value:40, Nonce:4, Timestamp:1563543006},        "tx5": Transaction{Value:50, Nonce:4, Timestamp:1563543005},        "tx6": Transaction{Value:60, Nonce:2, Timestamp:1563543005},        "tx7": Transaction{Value:70, Nonce:1, Timestamp:1563543006},    }    keys := make([]string, 0, len(memPool))        for key := range memPool {            keys = append(keys, key)        }        sort.Slice(keys, func(i, j int) bool { return memPool[keys[i]].Timestamp > memPool[keys[j]].Timestamp })    for _, v := range keys {        fmt.Println(v)    }    fmt.Println("")    keys2 := make([]string, 0, len(memPool))        for key2 := range memPool {            keys2 = append(keys2, key2)        }    sort.Slice(keys2, func(i, j int) bool { return memPool[keys2[i]].Nonce > memPool[keys2[j]].Nonce })    for _, v := range keys2 {        fmt.Println(v)    }}
查看完整描述

2 回答

?
蝴蝶刀刀

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

當(dāng)您似乎想要排序一次時(shí),您正在分別排序兩次。因此,使用您想要用于對值進(jìn)行排序的所有邏輯進(jìn)行一次排序。


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

    if memPool[keys[i]].Timestamp == memPool[keys[j]].Timestamp {

        if memPool[keys[i]].Nonce == memPool[keys[j]].Nonce {

            return memPool[keys[i]].Value < memPool[keys[j]].Value

        }

        return memPool[keys[i]].Nonce < memPool[keys[j]].Nonce

    }

    return memPool[keys[i]].Timestamp < memPool[keys[j]].Timestamp

})

工作示例: https: //play.golang.org/p/GERCSchEtOf


查看完整回答
反對 回復(fù) 2023-07-04
?
智慧大石

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

立即比較兩個(gè)搜索條件:


package main


import (

    "fmt"

    "sort"

)


type Transaction struct {

    Value         uint64                        `json:"value"`

    Nonce         uint64                        `json:"nonce"`

    Timestamp     int64                         `json:"timestamp"`

}


func main() {

    // To create a map as input

    memPool := map[string]Transaction {

        "tx1": Transaction{Value:10, Nonce:1, Timestamp:1563543005},

        "tx2": Transaction{Value:20, Nonce:2, Timestamp:1563543005},

        "tx3": Transaction{Value:30, Nonce:3, Timestamp:1563543006},

        "tx4": Transaction{Value:40, Nonce:4, Timestamp:1563543006},

        "tx5": Transaction{Value:50, Nonce:4, Timestamp:1563543005},

        "tx6": Transaction{Value:60, Nonce:2, Timestamp:1563543005},

        "tx7": Transaction{Value:70, Nonce:1, Timestamp:1563543006},

    }


    keys := make([]string, 0, len(memPool))

    for key := range memPool {

        keys = append(keys, key)

    }


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

        ti, tj := memPool[keys[i]], memPool[keys[j]]

        if ti.Timestamp == tj.Timestamp {

            return ti.Nonce < tj.Nonce

        }

        return ti.Timestamp < tj.Timestamp

    })


    for _, key := range keys {

        fmt.Println(memPool[key])

    }

}

https://play.golang.org/p/oFDG9Fti2JV


輸出:


{10 1 1563543005}

{60 2 1563543005}

{20 2 1563543005}

{50 4 1563543005}

{70 1 1563543006}

{30 3 1563543006}

{40 4 1563543006}

less func(i, j int) bool觀察參數(shù) to的實(shí)現(xiàn)方式sort.Slice:由于它必須首先按時(shí)間戳排序,然后按隨機(jī)數(shù)排序,因此必須考慮隨機(jī)數(shù)的唯一情況是時(shí)間戳相等時(shí)(否則它們已經(jīng)定義了要比較的元素的順序)。


查看完整回答
反對 回復(fù) 2023-07-04
  • 2 回答
  • 0 關(guān)注
  • 168 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號(hào)

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