如何对这种类型的Go语言进行排序。

huangapple go评论78阅读模式
英文:

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 (
&quot;fmt&quot;
&quot;sort&quot;
)
type sSortdata struct {
Key   string `json:&quot;key&quot;`
Value string `json:&quot;value&quot;`
}
type Data struct {
Id   int64       `json:&quot;id&quot;`
Sort []sSortdata `json:&quot;sort&quot;`
}
func main() {
m := []Data{
{
Id: 17878,
Sort: []sSortdata{
{
Key:   &quot;sort_order&quot;,
Value: &quot;5&quot;,
},
{
Key:   &quot;promoId&quot;,
Value: &quot;1&quot;,
},
},
},
{
Id: 17878,
Sort: []sSortdata{
{
Key:   &quot;sort_order&quot;,
Value: &quot;1&quot;,
},
{
Key:   &quot;promoId&quot;,
Value: &quot;1&quot;,
},
},
}}
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 &lt; 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}]}]
}

huangapple
  • 本文由 发表于 2021年8月20日 11:35:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/68856727.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定