Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/go/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sorting 按值映射排序(结构的属性)_Sorting_Go_Struct - Fatal编程技术网

Sorting 按值映射排序(结构的属性)

Sorting 按值映射排序(结构的属性),sorting,go,struct,Sorting,Go,Struct,我有下面的地图: detail := make(map[string]*Log) type Log struct { Id []string Name []string Priority int // value could be 1, 2, 3 Message string } 我想根据值对“detail”映射进行排序,在我的例子中,该值是一个结构。这应该按属性“优先级”排序 例如,Log(结构映射)的值可能与以下类似: Z :

我有下面的地图:

detail := make(map[string]*Log)

type Log struct {
    Id      []string
    Name    []string
    Priority   int   // value could be 1, 2, 3
    Message    string
}
我想根据值对“detail”映射进行排序,在我的例子中,该值是一个结构。这应该按属性“优先级”排序

例如,Log(结构映射)的值可能与以下类似:

Z : &{[ba60] [XYZ] 3 "I am the boss"}
B : &{[ca50] [ABC] 2 "I am the Junior"}
U : &{[zc20] [PQR] 1 "I am the Newbie"}
我希望它们从优先级增加的顺序(即1到3)打印

U : &{[zc20] [PQR] 1 "I am the Newbie"}
B : &{[ca50] [ABC] 2 "I am the Junior"}
Z : &{[ba60] [XYZ] 3 "I am the boss"}
我试图使用sort并实现了sort接口,但似乎仍然没有找到线索。因此,我实现了以下接口:

type byPriority []*Log

func (d byPriority) Len() int {
    return len(d)
}
func (d byPriority) Less(i, j int) bool {
    return d[i].Priority < d[j].Priority
}
func (d byPriority) Swap(i, j int) {
    d[i], d[j] = d[j], d[i]
}
type byPriority[]*Log
func(d byPriority)Len()int{
返回len(d)
}
func(d byPriority)减去(i,j int)bool{
返回d[i]。优先级

但是我应该如何在这个映射上应用sort.sort()方法来获得排序结果呢。是否需要添加更多代码?

Go中的
map
类型无序。不管你对一个贴图做了什么,下一次你在它上面迭代的时候,你会收到随机顺序的键。因此,无法对
map
进行“排序”

您可以做的是将映射的条目复制到一个可排序的切片中

package main

import (
    "fmt"
    "sort"
)

type Log struct {
    Id       []string
    Name     []string
    Priority int // value could be 1, 2, 3
    Message  string
}

type Entry struct {
    key   string
    value *Log
}

type byPriority []Entry

func (d byPriority) Len() int {
    return len(d)
}
func (d byPriority) Less(i, j int) bool {
    return d[i].value.Priority < d[j].value.Priority
}
func (d byPriority) Swap(i, j int) {
    d[i], d[j] = d[j], d[i]
}

func printSorted(detail map[string]*Log) {
    // Copy entries into a slice.
    slice := make(byPriority, 0, len(detail))
    for key, value := range detail {
        slice = append(slice, Entry{key, value})
    }

    // Sort the slice.
    sort.Sort(slice)

    // Iterate and print the entries in sorted order.
    for _, entry := range slice {
        fmt.Printf("%s : %v\n", entry.key, entry.value)
    }
}

func main() {
    detail := map[string]*Log{
        "Z": &Log{[]string{"ba60"}, []string{"XYZ"}, 3, "I am the boss"},
        "B": &Log{[]string{"ca50"}, []string{"ABC"}, 2, "I am the Junior"},
        "U": &Log{[]string{"zc20"}, []string{"PQR"}, 1, "I am the Newbie"},
    }

    printSorted(detail)
}
主程序包
进口(
“fmt”
“排序”
)
类型日志结构{
Id[]字符串
名称[]字符串
优先级int//值可以是1、2、3
消息字符串
}
类型入口结构{
键串
值*日志
}
键入byPriority[]项
func(d byPriority)Len()int{
返回len(d)
}
func(d byPriority)减去(i,j int)bool{
返回d[i].value.Priority
var x byPriority=/*something*/;sort.sort(x)
当然,我做了与上面相同的操作,因为我已经有了这个界面。但我如何在地图上应用它来获得预期的输出呢?你不能对地图进行排序。如果您的意思是,如何根据映射中的值对结构进行排序,然后只修改
Less
函数。但是,从你的问题来看,你想完成什么还不清楚。我已经清楚地提到了输入映射和输出映射,我可能做得不对,我想在值是结构的情况下对映射进行排序。所以,如果我想要排序的输出,我必须将map转换为struct,然后再进行排序?您已经在这样做了。那么你的问题是什么?非常感谢,这很有道理。这将有助于我从Python和Perl中学习如何使用嵌套数据结构。