亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

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

如何按兩個值對地圖進行排序?

如何按兩個值對地圖進行排序?

Go
千萬里不及你 2023-07-04 17:29:20
我正在嘗試按地圖的兩個值對地圖進行排序。首先是時間戳,然后是隨機數。換句話說,我需要能夠迭代并首先打印具有最小時間戳的映射,然后是隨機數值。像這樣:    "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貢獻1801條經驗 獲得超8個贊

當您似乎想要排序一次時,您正在分別排序兩次。因此,使用您想要用于對值進行排序的所有邏輯進行一次排序。


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


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

TA貢獻1946條經驗 獲得超3個贊

立即比較兩個搜索條件:


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觀察參數 to的實現方式sort.Slice:由于它必須首先按時間戳排序,然后按隨機數排序,因此必須考慮隨機數的唯一情況是時間戳相等時(否則它們已經定義了要比較的元素的順序)。


查看完整回答
反對 回復 2023-07-04
  • 2 回答
  • 0 關注
  • 170 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號