英文:
Sort the inner array using LINQ
问题
我有一个两层的数组,外层已经按照shopid进行分组和排序,我想按照价格降序对内层进行排序,但不改变外层的顺序。
// 内层结构体
type product struct {
name string
price int64
shopId int64
}
// 外层结构体
type oneGroup struct {
shopId int64
products []*product
}
// 最终的外层数组
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
// 根据分组后每组中最大价格降序排序,对外层进行分组和排序
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)
英文:
I have a two-layer array, the outer layer is already grouped and sorted by shopid, and I want to sort the inner layer by decreasing price without changing the order of the outer layer。
// 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
The input to linq should be [groupOut]
I took advantage of linq to group and sort the outer layer (Grouping and sorting outer layers in descending order according to the largest price in the group after grouping). But didn't know what to do with the inner layer.
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)
答案1
得分: 1
首先,按照shopId和价格降序构建一个有序的产品查询。
这样可以相应地对每个组的内部数组进行排序。
package main
import (
"fmt"
"github.com/ahmetb/go-linq/v3"
)
// 内部结构体
type product struct {
name string
price int64
shopId int64
}
func (x product) String() string { return fmt.Sprintf("<%s, [%d]> ", x.name, x.price) }
// 外部结构体
type oneGroup struct {
shopId int64
products []*product
}
// 最终的外部数组
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
// 首先按照shopId和价格降序排序产品
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
})
// 按照shopId分组并按照组内最大价格降序排序外层
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)
}
英文:
First, build an ordered query of products sorted by shopId and then by price in descending order.
This allows for the inner array of each group to be sorted accordingly.
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)
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论