如何按两个值对地图进行排序?

我正在尝试按地图的两个值对地图进行排序。首先是时间戳,然后是随机数。换句话说,我需要能够迭代并首先打印具有最小时间戳的映射,然后是随机数值。像这样:


    "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/hXo5clCrlU1


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 { 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)

    }


}


千万里不及你
浏览 124回答 2
2回答

蝴蝶刀刀

当您似乎想要排序一次时,您正在分别排序两次。因此,使用您想要用于对值进行排序的所有逻辑进行一次排序。sort.Slice(keys, func(i, j int) bool {&nbsp; &nbsp; if memPool[keys[i]].Timestamp == memPool[keys[j]].Timestamp {&nbsp; &nbsp; &nbsp; &nbsp; if memPool[keys[i]].Nonce == memPool[keys[j]].Nonce {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return memPool[keys[i]].Value < memPool[keys[j]].Value&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return memPool[keys[i]].Nonce < memPool[keys[j]].Nonce&nbsp; &nbsp; }&nbsp; &nbsp; return memPool[keys[i]].Timestamp < memPool[keys[j]].Timestamp})工作示例: https: //play.golang.org/p/GERCSchEtOf

智慧大石

立即比较两个搜索条件:package mainimport (&nbsp; &nbsp; "fmt"&nbsp; &nbsp; "sort")type Transaction struct {&nbsp; &nbsp; Value&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;uint64&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; `json:"value"`&nbsp; &nbsp; Nonce&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;uint64&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; `json:"nonce"`&nbsp; &nbsp; Timestamp&nbsp; &nbsp; &nbsp;int64&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;`json:"timestamp"`}func main() {&nbsp; &nbsp; // To create a map as input&nbsp; &nbsp; memPool := map[string]Transaction {&nbsp; &nbsp; &nbsp; &nbsp; "tx1": Transaction{Value:10, Nonce:1, Timestamp:1563543005},&nbsp; &nbsp; &nbsp; &nbsp; "tx2": Transaction{Value:20, Nonce:2, Timestamp:1563543005},&nbsp; &nbsp; &nbsp; &nbsp; "tx3": Transaction{Value:30, Nonce:3, Timestamp:1563543006},&nbsp; &nbsp; &nbsp; &nbsp; "tx4": Transaction{Value:40, Nonce:4, Timestamp:1563543006},&nbsp; &nbsp; &nbsp; &nbsp; "tx5": Transaction{Value:50, Nonce:4, Timestamp:1563543005},&nbsp; &nbsp; &nbsp; &nbsp; "tx6": Transaction{Value:60, Nonce:2, Timestamp:1563543005},&nbsp; &nbsp; &nbsp; &nbsp; "tx7": Transaction{Value:70, Nonce:1, Timestamp:1563543006},&nbsp; &nbsp; }&nbsp; &nbsp; keys := make([]string, 0, len(memPool))&nbsp; &nbsp; for key := range memPool {&nbsp; &nbsp; &nbsp; &nbsp; keys = append(keys, key)&nbsp; &nbsp; }&nbsp; &nbsp; sort.Slice(keys, func(i, j int) bool {&nbsp; &nbsp; &nbsp; &nbsp; ti, tj := memPool[keys[i]], memPool[keys[j]]&nbsp; &nbsp; &nbsp; &nbsp; if ti.Timestamp == tj.Timestamp {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return ti.Nonce < tj.Nonce&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return ti.Timestamp < tj.Timestamp&nbsp; &nbsp; })&nbsp; &nbsp; for _, key := range keys {&nbsp; &nbsp; &nbsp; &nbsp; fmt.Println(memPool[key])&nbsp; &nbsp; }}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:由于它必须首先按时间戳排序,然后按随机数排序,因此必须考虑随机数的唯一情况是时间戳相等时(否则它们已经定义了要比较的元素的顺序)。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go