英文:
how to sort this type go lang
问题
以下是翻译好的内容:
type sSortdata struct {
Key string `json:"key"`
Value string `json:"value"`
}
type Data struct {
Id int64 `json:"id"`
Sort []sSortdata `json:"sort"`
}
我想要使用只有 sort_order 类型的方式对 Data 结构进行排序。sSortdata 不是全局变量。有什么简单的方法可以做到这一点?
英文:
common": [
{
"id": 17878,
"name": "sk",
"sort": [
{
"key": "sort_order",
"value": "5"
}
{
"key": "promoId",
"value": "1"
}
]
}
{
"id": 17878,
"name": "sk",
"sort": [
{
"key": "sort_order",
"value": "1"
}
{
"key": "promoId",
"value": "1"
}
]
}
]
type sSortdata struct {
Key string `json:"key"`
Value string `json:"value"`
}
type Data struct {
Id int64 `json:"id"`
Sort []sSortdata `json:"sort"`
}
I want to sort Data struct using using only sort_order type . sSortdata is not global variable . How can do this easy way
答案1
得分: 2
你可以使用sort.Slice()
函数对切片进行排序。由于排序回调函数是由应用程序提供的,变量作用域不会影响排序。
在playground上运行
package main
import (
"fmt"
"sort"
)
type sSortdata struct {
Key string `json:"key"`
Value string `json:"value"`
}
type Data struct {
Id int64 `json:"id"`
Sort []sSortdata `json:"sort"`
}
func main() {
m := []Data{
{
Id: 17878,
Sort: []sSortdata{
{
Key: "sort_order",
Value: "5",
},
{
Key: "promoId",
Value: "1",
},
},
},
{
Id: 17878,
Sort: []sSortdata{
{
Key: "sort_order",
Value: "1",
},
{
Key: "promoId",
Value: "1",
},
},
}}
fmt.Println("排序前 := ", m)
sort.Slice(m, func(i, j int) bool {
var sortI, sortJ string
for _, data := range m[i].Sort {
if data.Key == "sort_order" {
sortI = data.Value
break
}
}
for _, data := range m[j].Sort {
if data.Key == "sort_order" {
sortJ = data.Value
break
}
}
return sortI < sortJ
})
fmt.Println("排序后 := ", m)
//排序前 := [{17878 [{sort_order 5} {promoId 1}]} {17878 [{sort_order 1} {promoId 1}]}]
//排序后 := [{17878 [{sort_order 1} {promoId 1}]} {17878 [{sort_order 5} {promoId 1}]}]
}
英文:
You can use sort.Slice()
function to sort a slice. Because that sorting callback function given by the application, variable scope didn't affect to the sorting.
run on playground
package main
import (
"fmt"
"sort"
)
type sSortdata struct {
Key string `json:"key"`
Value string `json:"value"`
}
type Data struct {
Id int64 `json:"id"`
Sort []sSortdata `json:"sort"`
}
func main() {
m := []Data{
{
Id: 17878,
Sort: []sSortdata{
{
Key: "sort_order",
Value: "5",
},
{
Key: "promoId",
Value: "1",
},
},
},
{
Id: 17878,
Sort: []sSortdata{
{
Key: "sort_order",
Value: "1",
},
{
Key: "promoId",
Value: "1",
},
},
}}
fmt.Println(`before sorting := `, m)
sort.Slice(m, func(i, j int) bool {
var sortI, sortJ string
for _, data := range m[i].Sort {
if data.Key == `sort_order` {
sortI = data.Value
break
}
}
for _, data := range m[j].Sort {
if data.Key == `sort_order` {
sortJ = data.Value
break
}
}
return sortI < sortJ
})
fmt.Println(`after sorting := `, m)
//before sorting := [{17878 [{sort_order 5} {promoId 1}]} {17878 [{sort_order 1} {promoId 1}]}]
//after sorting := [{17878 [{sort_order 1} {promoId 1}]} {17878 [{sort_order 5} {promoId 1}]}]
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论