2011-12-17 32 views
1

我想按存儲的關鍵字對文章進行分類。我有一個類別的關鍵字列表,並且我想要一篇文章分配一個關鍵字數最多的類別。如何對非唯一值列表中的對象進行排序?

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) 

也許我這樣做都是錯的,但到目前爲止,我認爲我是在正確的道路上。 (我也有一個類別類的默認比較,它只是沒有工作。)

我得到最有可能造成的排序異常,因爲它們不是唯一的。

我得到的異常是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 

回答

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 
+0

可能工作...雖然我使用LINQ做我現在整理。非常容易使用,不需要使用比較器的東西。 – JustinKaz 2011-12-19 01:26:00

1

我發現用的是微軟的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

相關問題