我有一个两层数组,外层已经按shopid分组和排序,我想在不改变外层顺序的情况下通过降低价格对内层进行排序。
// Inner struct
type product struct {
name string
price int64
shopId int64
}
// Outer struct
type oneGroup struct {
shopId int64
products []*product
}
// Final Outer array
var groupOut []oneGroup
linq的输入应该是[groupOut]
我利用linq对外层进行分组和排序(分组后按照组中最大的价格降序对外层进行分组和排序)。但不知道该怎么处理内层。
products := make([]*product, 0, 20)
products = append(products, &product{name: "product66", price: 6666, shopId: 100})
products = append(products, &product{name: "product11", price: 1111, shopId: 100})
products = append(products, &product{name: "product44", price: 4444, shopId: 100})
products = append(products, &product{name: "product22", price: 2222, shopId: 200})
products = append(products, &product{name: "product77", price: 9999, shopId: 200})
products = append(products, &product{name: "product00", price: 0000, shopId: 200})
products = append(products, &product{name: "product33", price: 3333, shopId: 300})
products = append(products, &product{name: "product88", price: 8888, shopId: 300})
products = append(products, &product{name: "product55", price: 5555, shopId: 300})
products = append(products, &product{name: "product99", price: 7777, shopId: 400})
var query []linq.Group
//Grouping and sorting outer layers in descending order according to the largest price in the group after grouping
linq.From(products).GroupByT(
func(p *product) int64 { return p.shopId },
func(p *product) *product { return p },
).OrderByT(
func(g linq.Group) int64 {
//return g.Key.(int64)
return linq.From(g.Group).Select(
func(x interface{}) interface{} { return x.(*product).price },
).Max().(int64)
},
).ToSlice(&query)
首先,构建一个按shopId和价格降序排序的有序查询。
允许对每个组的内部数组进行相应的排序。
package main
import (
"fmt"
"github.com/ahmetb/go-linq/v3"
)
// Inner struct
type product struct {
name string
price int64
shopId int64
}
func (x product) String() string { return fmt.Sprintf("<%s, [%d]> ", x.name, x.price) }
// Outer struct
type oneGroup struct {
shopId int64
products []*product
}
// Final Outer array
var groupOut []oneGroup
func main() {
products := make([]*product, 0, 20)
products = append(products, &product{name: "product66", price: 6666, shopId: 100})
products = append(products, &product{name: "product11", price: 1111, shopId: 100})
products = append(products, &product{name: "product44", price: 4444, shopId: 100})
products = append(products, &product{name: "product22", price: 2222, shopId: 200})
products = append(products, &product{name: "product77", price: 9999, shopId: 200})
products = append(products, &product{name: "product00", price: 0000, shopId: 200})
products = append(products, &product{name: "product33", price: 3333, shopId: 300})
products = append(products, &product{name: "product88", price: 8888, shopId: 300})
products = append(products, &product{name: "product55", price: 5555, shopId: 300})
products = append(products, &product{name: "product99", price: 7777, shopId: 400})
var query []linq.Group
// Sort products first by shopId and the price in descending order
var productsByShopIdAndPrice linq.OrderedQuery = linq.From(products).OrderByT(func(p interface{}) int64 {
return p.(*product).shopId
}).ThenByDescending(func(p interface{}) interface{} {
return p.(*product).price
})
// Grouping and sorting outer layers in descending order according to the largest price in the group after grouping
productsByShopIdAndPrice.GroupByT(
func(p *product) int64 { return p.shopId },
func(p *product) *product { return p },
).OrderByT(
func(g linq.Group) int64 {
//return g.Key.(int64)
return linq.From(g.Group).Select(
func(x interface{}) interface{} { return x.(*product).price },
).Max().(int64)
},
).ToSlice(&query)
fmt.Printf("%v n", query)
}