dongnunai3125 2017-03-29 10:25
浏览 114
已采纳

排序结构数组(缺少Len方法)

I have two structs defined as below:

// DuplicatedAd format
type DuplicatedAd struct {
    // Ad Identifier
    AdID int `form:"ad_id" json:"ad_id,omitempty" xml:"ad_id"`
    // Parameters of an Ad, can be empty
    AdParams string `form:"ad_params" json:"ad_params,omitempty" xml:"ad_params"`
    // Body of the Ad
    Body string `form:"body" json:"body,omitempty" xml:"body"`
    // Body Duplicate Percentage
    BodyScore float64 `form:"body_score" json:"body_score,omitempty" xml:"body_score"`
    // Category of the Ad
    Category int `form:"category" json:"category,omitempty" xml:"category"`
    // The ad uploaded main image
    Image string `form:"image,omitempty" json:"image,omitempty" xml:"image,omitempty"`
    // Ad Listing ID
    ListID int `form:"list_id" json:"list_id,omitempty" xml:"list_id"`
    // Listing time for the ad
    ListTime string `form:"list_time" json:"list_time,omitempty" xml:"list_time"`
    // the ad Phone number
    Phone string `form:"phone,omitempty" json:"phone,omitempty" xml:"phone,omitempty"`
    // Region of the Ad
    Region int `form:"region" json:"region,omitempty" xml:"region"`
    // Ad Status
    Status string `form:"status" json:"status,omitempty" xml:"status"`
    // Subject of the Ad
    Subject string `form:"subject" json:"subject,omitempty" xml:"subject"`
    // Subject Duplicate Percentage
    SubjectScore float64 `form:"subject_score" json:"subject_score,omitempty" xml:"subject_score"`
    // Type of the Ad
    Type string `form:"type" json:"type,omitempty" xml:"type"`
    // Total Score
    TotalScore float64 `form:"total_score" json:"total_score,omitempty" xml:"total_score"`
}

// CpResponse format
type CpResponse struct {
    // Number of doublets
    Count int `json:"count"`
    // The Doublet Ads to show
    Duplicated []DuplicatedAd `json:"duplicated"`
}

i'm defining a variable var DuplicatedAds = CpResponse{}and in a loop i'm appending some results to it as follow:

DuplicatedAds.Duplicated = append(DuplicatedAds.Duplicated, t)
DuplicatedAds.Count = len(DuplicatedAds.Duplicated)

i wish that the DuplicatedAds.Duplicated be sorted by TotalScore.

i'm new to golang, so after a little research i found out that i must implement theses three functions before i can sort

func (slice DuplicatedAd) Len() int {
    return len(slice)
}

func (slice DuplicatedAd) Less(i, j int) bool {
    return slice[i].TotalScore < slice[j].TotalScore
}

func (slice DuplicatedAd) Swap(i, j int) {
    slice[i], slice[j] = slice[j], slice[i]
}

but when i try to compile i get this errors:

./duplicates.go:165: cannot use DuplicatedAds.Duplicated (type []DuplicatedAd) as type sort.Interface in argument to sort.Sort:
    []DuplicatedAd does not implement sort.Interface (missing Len method)
./types.go:68: invalid argument slice (type DuplicatedAd) for len
./types.go:72: invalid operation: slice[i] (type DuplicatedAd does not support indexing)
./types.go:72: invalid operation: slice[j] (type DuplicatedAd does not support indexing)
./types.go:76: invalid operation: slice[i] (type DuplicatedAd does not support indexing)
./types.go:76: invalid operation: slice[j] (type DuplicatedAd does not support indexing)
./types.go:76: invalid operation: slice[i] (type DuplicatedAd does not support indexing)

How could i have an CpResponse object which his Duplicated array is sorted by DuplicatedAd.TotalScore ?

  • 写回答

2条回答 默认 最新

  • drkrsx3135168 2017-03-29 12:04
    关注

    Starting from Go 1.8, you can use the following function to sort your slice:

    sort.Slice(DuplicatedAds.Duplicated, func(i int, j int) bool {
        return DuplicatedAds.Duplicated[i].TotalScore < DuplicatedAds.Duplicated[j].TotalScore
    })
    

    You don't need to define those three functions up there anymore (Len, Less and Swap). Notice how we used the same implementation of Len as a parameter of sort.Slice.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥15 用matlab 设计一个不动点迭代法求解非线性方程组的代码
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度