I am trying to sort a map by two of its values. First by the timestamp and then by the nonce. In other words, I need to be able to iterate and print the map with the smallest timestamp first, followed by the nonce value. Like this:
"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},
Here is my code:
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)
}
}
Current output:
tx7
tx3
tx4
tx1
tx2
tx5
tx6
tx4
tx5
tx3
tx6
tx2
tx7
tx1
Desired output:
tx1
tx2
tx6
tx5
tx7
tx3
tx4
You're sorting two separate times when it appears you want to sort once. So, sort once, using all of the logic that you want to use to sort your values, once.
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
})
Working example: https://play.golang.org/p/GERCSchEtOf
Compare both search criteria at once:
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
Output:
{10 1 1563543005}
{60 2 1563543005}
{20 2 1563543005}
{50 4 1563543005}
{70 1 1563543006}
{30 3 1563543006}
{40 4 1563543006}
Observe how the less func(i, j int) bool
argument to sort.Slice
is implemented: since it has to sort by timestamps first and nonces second, the only case where it has to consider nonces is when timestamps are equal (as otherwise they already define ordering of the elements being compared).