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
Sorting GoLang中的排序对_Sorting_Go_Key_Key Value - Fatal编程技术网

Sorting GoLang中的排序对

Sorting GoLang中的排序对,sorting,go,key,key-value,Sorting,Go,Key,Key Value,我知道如何使用数据类型对键/值进行排序: map[1:a 2:c 0:b] 使用GoLang的sort包。如何按如下方式对对进行排序: [{c 2} {a 1} {b 0}] 我希望整对按照键或值进行排序?最终结果: [{a 1} {b 0} {c 2}] 这是根据键排序的。以下是根据值排序的: [{b 0} {a 1} {c 2}] 您可以为自定义类型实现Len、Swap和Less。此处给出了一个示例: 以下是如何按键对示例进行排序: 导入( “fmt” “排序” ) 类型对结构{ 键

我知道如何使用数据类型对键/值进行排序:

map[1:a 2:c 0:b]
使用GoLang的
sort
包。如何按如下方式对
进行排序:

[{c 2} {a 1} {b 0}]
我希望整对按照键或值进行排序?最终结果:

[{a 1} {b 0} {c 2}]
这是根据键排序的。以下是根据值排序的:

[{b 0} {a 1} {c 2}]

您可以为自定义类型实现
Len
Swap
Less
。此处给出了一个示例:

以下是如何按键对示例进行排序:

导入(
“fmt”
“排序”
)
类型对结构{
键串
值int
}
键入ByKey[]对
func(s ByKey)Len()int{
返回透镜(s)
}
func(s ByKey)交换(i,j int){
s[i],s[j]=s[j],s[i]
}
func(s ByKey)减去(i,j int)bool{
返回s[i]。Key
您可以为自定义类型实现
Len
Swap
Less
。此处给出了一个示例:

以下是如何按键对示例进行排序:

导入(
“fmt”
“排序”
)
类型对结构{
键串
值int
}
键入ByKey[]对
func(s ByKey)Len()int{
返回透镜(s)
}
func(s ByKey)交换(i,j int){
s[i],s[j]=s[j],s[i]
}
func(s ByKey)减去(i,j int)bool{
返回s[i]。Key
您可以为自定义类型实现
Len
Swap
Less
。此处给出了一个示例:

以下是如何按键对示例进行排序:

导入(
“fmt”
“排序”
)
类型对结构{
键串
值int
}
键入ByKey[]对
func(s ByKey)Len()int{
返回透镜(s)
}
func(s ByKey)交换(i,j int){
s[i],s[j]=s[j],s[i]
}
func(s ByKey)减去(i,j int)bool{
返回s[i]。Key
您可以为自定义类型实现
Len
Swap
Less
。此处给出了一个示例:

以下是如何按键对示例进行排序:

导入(
“fmt”
“排序”
)
类型对结构{
键串
值int
}
键入ByKey[]对
func(s ByKey)Len()int{
返回透镜(s)
}
func(s ByKey)交换(i,j int){
s[i],s[j]=s[j],s[i]
}
func(s ByKey)减去(i,j int)bool{
返回s[i]。Key
我不希望将键/值单独排序并显示它们,我希望将整对键/值一起排序。请看我的编辑。@user2567857,我相信我理解你的意思。我在回答中添加了一个示例链接。如果有人想知道如何根据
value
进行排序,只需更改
s[I]。Key
s[j]。Key
s[I]。value
s[I]。value
分别进行排序。我不希望单独对Key/value进行排序并显示它们,我希望整对Key/value进行排序。请看我的编辑。@user2567857,我相信我理解你的意思。我在回答中添加了一个示例链接。如果有人想知道如何根据
value
进行排序,只需更改
s[I]。Key
s[j]。Key
s[I]。value
s[I]。value
分别进行排序。我不希望单独对Key/value进行排序并显示它们,我希望整对Key/value进行排序。请看我的编辑。@user2567857,我相信我理解你的意思。我在回答中添加了一个示例链接。如果有人想知道如何根据
value
进行排序,只需更改
s[I]。Key
s[j]。Key
s[I]。value
s[I]。value
分别进行排序。我不希望单独对Key/value进行排序并显示它们,我希望整对Key/value进行排序。请看我的编辑。@user2567857,我相信我理解你的意思。我在我的答案中添加了一个示例链接。如果有人想知道如何根据
value
进行排序,只需分别更改
s[I]。Key
s[j]。Key
s[I]。value
s[I]。value
import (
    "fmt"
    "sort"
)

type Pair struct {
    Key   string
    Value int
}

type ByKey []Pair

func (s ByKey) Len() int {
    return len(s)
}

func (s ByKey) Swap(i, j int) {
    s[i], s[j] = s[j], s[i]
}

func (s ByKey) Less(i, j int) bool {
    return s[i].Key < s[j].Key
}

func main() {
    pairs := []Pair{{"a", 1}, {"b", 0}, {"c", 2}}
    // Sort by Key
    sort.Sort(ByKey(pairs))
    fmt.Println(pairs) // [{a 1} {b 0} {c 2}]
}