当前位置:网站首页>Go grouping & sorting
Go grouping & sorting
2022-06-23 05:24:00 【Zen and the art of computer programming】
func (r *MultiMarketOverviewService) GetMultiMarketSummaryPriceBandDistributionDataTable(ctx context.Context, req insight.MultiMarketSummaryPriceBandDistributionDataTableReq) response.Response {
rpcReq := &multi_market_overview.MultiMarketSummaryPriceBandDistributionDataTableReq{}
copier.Copy(rpcReq, &req)
rpcResp, err := caller.CompassInsightClient.GetMultiMarketSummaryPriceBandDistributionDataTable(ctx, rpcReq)
if err != nil {
return response.JSONSt(define.StRPCErr)
}
data := rpcResp.Data
// according to Dimention grouping , Sort by price band
sortDataTable(&data)
return response.JSONData(data)
}
func sortDataTable(data **multi_market_overview.MultiMarketSummaryPriceBandDistributionDatatableData) {
var list []*multi_market_overview.MultiMarketSummaryPriceBandDistributionDataTable = (*data).Datatable
// grouping
var groups = make(map[string][]*multi_market_overview.MultiMarketSummaryPriceBandDistributionDataTable)
for _, item := range list {
groups[item.Dimention] = append(groups[item.Dimention], item)
}
// In group sorting
for _, dataTables := range groups {
// Sort from small to large ( Stable sequencing )
sort.SliceStable(dataTables, func(i, j int) bool {
if GetPriceValue(dataTables[i].PriceBrand) < GetPriceValue(dataTables[j].PriceBrand) {
return true
}
return false
})
}
res := make([]*multi_market_overview.MultiMarketSummaryPriceBandDistributionDataTable, 0)
for _, vlist := range groups {
res = append(res, vlist...)
}
*data = &multi_market_overview.MultiMarketSummaryPriceBandDistributionDatatableData{
Datatable: res,
}
}
// GetPriceValue Price with data analysis
func GetPriceValue(price string) int64 {
priceRegexp := regexp.MustCompile(`^([0-9]+).*`)
priceValues := priceRegexp.FindStringSubmatch(price)
if priceValues == nil || len(priceValues) <= 1 {
return 0
}
i, err := strconv.ParseInt(priceValues[1], 10, 64)
if err != nil {
return 0
}
return i
}among , The sorting code is go sdk Provided go1.16.4/src/sort/slice.go :
// SliceStable sorts the slice x using the provided less
// function, keeping equal elements in their original order.
// It panics if x is not a slice.
//
// The less function must satisfy the same requirements as
// the Interface type's Less method.
func SliceStable(x interface{}, less func(i, j int) bool) {
rv := reflectValueOf(x)
swap := reflectSwapper(x)
stable_func(lessSwap{less, swap}, rv.Len())
}
// Auto-generated variant of sort.go:stable
func stable_func(data lessSwap, n int) {
blockSize := 20
a, b := 0, blockSize
for b <= n {
insertionSort_func(data, a, b)
a = b
b += blockSize
}
insertionSort_func(data, a, n)
for blockSize < n {
a, b = 0, 2*blockSize
for b <= n {
symMerge_func(data, a, a+blockSize, b)
a = b
b += 2 * blockSize
}
if m := a + blockSize; m < n {
symMerge_func(data, a, m, n)
}
blockSize *= 2
}
}
package sort
// Auto-generated variant of sort.go:insertionSort
func insertionSort_func(data lessSwap, a, b int) {
for i := a + 1; i < b; i++ {
for j := i; j > a && data.Less(j, j-1); j-- {
data.Swap(j, j-1)
}
}
}边栏推荐
- Difficult to find a job in a bad environment? Ali on three sides. Fortunately, he has made full preparations and has offered
- A compiler related bug in rtklib2.4.3 B34
- Onnxoptimizer, onnxsim usage records
- JVM原理之完整的一次GC流程
- Memory model of JVM principle
- MCS:离散随机变量——Uniform分布
- Spark 离线开发框架设计与实现
- Ams:startactivity desktop launch application
- The tiobe programming language ranking is an indicator of the popular trend of programming languages
- 牛B程序员在“创建索引”时都会注意啥?
猜你喜欢

Hcip reissue experiment

JVM原理之内存模型

Penetration test basis | attached test points and test scenarios

Calculate Euclidean distance and cosine similarity

BGP experiment

Complete the primary school project in 3 days, and teach you to complete the weather broadcast system hand in hand!

JDBC入门学习(三)之事务回滚功能的实现

ES6的Array.from方法创建长度为N的undefined数组

九九乘法表.bat

Three methods of GNSS velocity calculation
随机推荐
JVM原理之完整的一次GC流程
BGP second test
ES6的Array.from方法创建长度为N的undefined数组
JDBC入门学习(二)之封装工具类
Introduction to MySQL (I) grammar
Snippet Manager snippetslab
MCS:离散随机变量——Bernoulli分布
Missing essential plugin
第九章 APP项目测试(1)
App hangs~
Web 应用程序安全测试指南
gis利器之Gdal(三)gdb数据读取
Hcip switch experiment
1010 Radix
GO语言-panic和recover
面对新的挑战,成为更好的自己--进击的技术er
OSPF shunt test
UnityShader入门精要——Unity中的渲染优化技术(四)
Sift特征点提取
Zygote进程