' genericalias.vb Imports System Imports System.Net Imports System.Text.RegularExpressions Imports System.Collections.Generic ' ジェネリック・クラスに対する別名の定義 Imports StrIntPair = System.Collections.Generic.KeyValuePair(Of String, Integer) Imports StrIntDict = System.Collections.Generic.Dictionary(Of String, Integer) Imports StrIntList = System.Collections.Generic.List(Of System.Collections.Generic.KeyValuePair(Of String, Integer)) '''''''''''''''''''''''''''''''''''''''''''''''''' Class DictionarySortByValue Shared Sub makeDict(ByVal dict As StrIntDict) Dim html As String ' HTMLの取得 Using wc As New WebClient() html = wc.DownloadString("http://www.atmarkit.co.jp/") End Using ' 単語に分割して、各単語の出現頻度をカウント For Each s As String In Regex.Split(html, "\W") Dim word As String = s.Trim() If Not word = "" Then If dict.ContainsKey(word) Then dict(word) += 1 Else dict(word) = 1 End If End If Next End Sub Shared Sub Main() Dim dict As New StrIntDict() makeDict(dict) Dim sorted As StrIntList = sortByValue(dict) ' sorted.Reverse() ' 逆順にする場合 For Each kvp As StrIntPair In sorted Console.WriteLine(kvp.Key & ":" & kvp.Value) Next ' 出力例: ' a:542 ' td:394 ' 0:328 ' width:289 ' tr:284 ' …… End Sub Shared Function hikaku(ByVal kvp1 As StrIntPair, ByVal kvp2 As StrIntPair) As Integer ' Valueの大きい順にソート Return kvp2.Value - kvp1.Value End Function Shared Function sortByValue(ByVal dict As StrIntDict) As StrIntList Dim list As New StrIntList(dict) list.Sort(AddressOf hikaku) Return list End Function End Class ' コンパイル方法:vbc genericalias.vb