当前位置:网站首页>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)
        }
    }
}
原网站

版权声明
本文为[Zen and the art of computer programming]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/174/202206230307401928.html