如何在非唯一值中对列表中的对象进行排序?

时间:2011-12-17 22:56:49

标签: vb.net list sorting icomparer

我正在尝试按存储的关键字对文章进行分类。我有一个类别的关键字列表,我想要一篇文章分配一个关键字数量最多的类别。

For Each keyword As String In category.Keywords
    category.tempCount += Regex.Matches(article.Item("title").InnerXml, Regex.Escape(keyword)).Count
    category.tempCount += Regex.Matches(article.Item("description").InnerXml, Regex.Escape(keyword)).Count
Next

这是为每个类别完成的,为每篇文章运行。我正在尝试对列表进行排序,以便确定哪个类别是本文的最佳类别。但是,有可能不止一个类别是最好的,并且没有一个类别适合。所以运行这个对我没有帮助:

Categories.Sort(
Function(article1 As ArticleCategory, article2 As ArticleCategory)
    Return article1.tempCount.CompareTo(article2.tempCount)
End Function)

也许我这样做是错的,但到目前为止,我认为我走的是正确的道路。 (我在Category类中也有一个默认比较,它也没有工作。)

我对排序很可能造成例外,因为它们不是唯一的。

我得到的异常是InvalidOperationException:无法比较数组中的两个元素。那就是使用我在ArticleClass中构建的比较器

Imports System.Xml

Class ArticleCategory
Implements IComparer(Of ArticleCategory)

Public ReadOnly key As Int32
Public ReadOnly Name As String
Public ReadOnly Keywords As List(Of String)
Public tempCount As Integer = 0

Public Sub New(ByVal category As XmlElement)
    key = System.Web.HttpUtility.UrlDecode(category.Item("ckey").InnerXml)
    Name = System.Web.HttpUtility.UrlDecode(category.Item("name").InnerXml)

    Dim tKeywords As Array = System.Web.HttpUtility.UrlDecode(category.Item("keywords").InnerXml).Split(",")
    Dim nKeywords As New List(Of String)
    For Each keyword As String In tKeywords
        If Not keyword.Trim = "" Then
            nKeywords.Add(keyword.Trim)
        End If
    Next

    Keywords = nKeywords
End Sub

'This should be removed if your using my solution.
Public Function Compare(ByVal x As ArticleCategory, ByVal y As ArticleCategory) As Integer Implements System.Collections.Generic.IComparer(Of ArticleCategory).Compare
    Return String.Compare(x.tempCount, y.tempCount)
End Function


End Class

2 个答案:

答案 0 :(得分:1)

您需要实施IComparable而不是IComparer

IComparer将由执行排序的类(例如List类)实现,而IComparable将由正在排序的类实现。

例如:

Public Function CompareTo(other As ArticleCategory) As Integer Implements System.IComparable(Of ArticleCategory).CompareTo
    Return Me.tempCount.CompareTo(other.tempCount)
End Function

答案 1 :(得分:1)

我发现最好的解决方案是使用Microsoft LINQ(对象的查询语言),它可以很好地工作并快速生成正确的结果。

Dim bestCat As ArticleCategory
bestCat = (From cat In Categories
           Order By cat.tempCount Descending, cat.Name
           Select cat).First

完成我的解决方案:

For Each category As ArticleCategory In Categories
    category.tempCount = 0

    For Each keyword As String In category.Keywords
        category.tempCount += Regex.Matches(System.Web.HttpUtility.UrlDecode(article.Item("title").InnerXml), Regex.Escape(keyword)).Count
        category.tempCount += Regex.Matches(System.Web.HttpUtility.UrlDecode(article.Item("description").InnerXml), Regex.Escape(keyword)).Count
    Next

Next

Dim bestCat As ArticleCategory

Try
    bestCat = (From cat In Categories
               Order By cat.tempCount Descending, cat.Name
               Select cat).First
Catch ex As Exception
    ReportStatus(ex.Message)
End Try

因此,这是我对列表对象或数组进行排序或查询的首选方法。它可以在最快的时间内产生最佳结果,而无需将IComparer实现添加到您的类中。

Microsoft.com

上查看