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

时间:2017-03-29 10:25:40

标签: arrays sorting go struct

我有两个结构定义如下:

// 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"`
}

我正在定义一个变量var DuplicatedAds = CpResponse{},并在一个循环中我将一些结果附加到它,如下所示:

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

我希望DuplicatedAds.DuplicatedTotalScore排序。

我是golang的新手,所以经过一番研究后我发现我必须先实现这三个功能才能排序

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]
}

但是当我尝试编译时我得到了这个错误:

./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)

我怎么能有一个CpResponse对象,Duplicated数组按DuplicatedAd.TotalScore排序?

2 个答案:

答案 0 :(得分:3)

从Go 1.8开始,您可以使用以下功能对切片进行排序:

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

您不再需要在那里定义这三个功能(LenLessSwap)。请注意我们如何使用Len的相同实现作为sort.Slice的参数。

答案 1 :(得分:0)

cd1提供了一个很好的解决方案。至于你的方法不起作用的原因,那是因为它们在单个结构上运行,而不是一块结构。

你会想要更像这样的东西,其中每个方法都适用于DuplicatedAdSlice类型而不是DuplicatedAd类型。

[(ngModel)]="searchText" (ngModelChange)="SearchTextChange()"

更新CpResponse结构以使用类型别名

type DuplicatedAdSlice []DuplicatedAd // Type alias

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