1 回答
TA贡献1829条经验 获得超7个赞
package main
import (
"fmt"
"github.com/ahmetb/go-linq"
)
type T struct {
Name string
Tags []string
}
func main() {
params := []string{"food", "music", "gaming", "sport"}
t := []T{
T{Name: "event1", Tags: []string{"food", "music", "gaming", "sport"}},
T{Name: "event2", Tags: []string{"gaming"}},
T{Name: "event3", Tags: []string{"music", "sport"}},
T{Name: "event4", Tags: []string{"food", "music", "gaming", "sport"}},
T{Name: "event5", Tags: []string{"music", "coding", "sport"}},
T{Name: "event6", Tags: []string{"coding"}},
T{Name: "event7", Tags: []string{"food", "gaming", "sport"}},
}
var result []T
linq.From(t).SortT(func(t1 T, t2 T) bool {
var rs1 []string
linq.From(t1.Tags).IntersectByT(linq.From(params), func(str string) string {
return str
}).ToSlice(&rs1)
var rs2 []string
linq.From(t2.Tags).IntersectByT(linq.From(params), func(str string) string {
return str
}).ToSlice(&rs2)
return len(rs1) > len(rs2)
}).ToSlice(&result)
fmt.Printf("%+v", result)
}
[{名称:event1 标签:[美食音乐游戏运动]} {名称:event4 标签:[美食音乐游戏运动]} {名称:event7 标签:[美食音乐游戏运动]} {名称:event3 标签:[音乐运动]} {名称:event5 标签:[音乐编码运动]} {名称:event2 标签:[游戏]} {名称:event6 标签:[编码]}]
上面的程序根据您的要求对数组进行排序,希望这会对您有所帮助。
- 1 回答
- 0 关注
- 117 浏览
添加回答
举报