【VB.NET】Dictionary按Value进行排序
VB.NET
' sortbyvalue.vb
Imports System
Imports System.Net
Imports System.Text.RegularExpressions
Imports System.Collections.Generic
Class DictionarySortByValue
Shared Sub makeDict(ByVal dict As Dictionary(Of String, Integer))
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 Dictionary(Of String, Integer)
makeDict(dict)
Dim sorted As List(Of KeyValuePair(Of String, Integer)) = sortByValue(dict)
' sorted.Reverse() ' 逆順にする場合
For Each kvp As KeyValuePair(Of String, Integer) 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 KeyValuePair(Of String, Integer), _
ByVal kvp2 As KeyValuePair(Of String, Integer)) As Integer
' Valueの大きい順にソート
Return kvp2.Value - kvp1.Value
End Function
Shared Function sortByValue( _
ByVal dict As Dictionary(Of String, Integer)) _
As List(Of KeyValuePair(Of String, Integer))
Dim list As New List(Of KeyValuePair(Of String, Integer))(dict)
list.Sort(AddressOf hikaku)
Return list
End Function
End Class
' コンパイル方法:vbc sortbyvalue.vb
Imports System
Imports System.Net
Imports System.Text.RegularExpressions
Imports System.Collections.Generic
Class DictionarySortByValue
Shared Sub makeDict(ByVal dict As Dictionary(Of String, Integer))
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 Dictionary(Of String, Integer)
makeDict(dict)
Dim sorted As List(Of KeyValuePair(Of String, Integer)) = sortByValue(dict)
' sorted.Reverse() ' 逆順にする場合
For Each kvp As KeyValuePair(Of String, Integer) 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 KeyValuePair(Of String, Integer), _
ByVal kvp2 As KeyValuePair(Of String, Integer)) As Integer
' Valueの大きい順にソート
Return kvp2.Value - kvp1.Value
End Function
Shared Function sortByValue( _
ByVal dict As Dictionary(Of String, Integer)) _
As List(Of KeyValuePair(Of String, Integer))
Dim list As New List(Of KeyValuePair(Of String, Integer))(dict)
list.Sort(AddressOf hikaku)
Return list
End Function
End Class
' コンパイル方法:vbc sortbyvalue.vb
C#
// sortbyvalue.cs
using System;
using System.Net;
using System.Text.RegularExpressions;
using System.Collections.Generic;
class DictionarySortByValue {
static void makeDict(Dictionary<string, int> dict) {
string html;
// HTMLの取得
using (WebClient wc = new WebClient()) {
html = wc.DownloadString("http://www.atmarkit.co.jp/");
}
// 単語に分割して、各単語の出現頻度をカウント
foreach (string s in Regex.Split(html, "\\W")) {
string word = s.Trim();
if (word != "") {
if (dict.ContainsKey(word)) {
dict[word]++;
} else {
dict[word] = 1;
}
}
}
}
static void Main() {
Dictionary<string, int> dict = new Dictionary<string, int>();
makeDict(dict);
List<KeyValuePair<string, int>> sorted = sortByValue(dict);
// sorted.Reverse(); // 逆順にする場合
foreach (KeyValuePair<string, int> kvp in sorted) {
Console.WriteLine(kvp.Key + ":" + kvp.Value);
}
// 出力例:
// a:542
// td:394
// 0:328
// width:289
// tr:284
// ……
}
static List<KeyValuePair<string, int>>
sortByValue(Dictionary<string, int> dict)
{
List<KeyValuePair<string, int>> list
= new List<KeyValuePair<string, int>>(dict);
// Valueの大きい順にソート
list.Sort(
delegate(KeyValuePair<string, int> kvp1, KeyValuePair<string, int> kvp2) {
return kvp2.Value - kvp1.Value;
});
return list;
}
}
// コンパイル方法:csc sortbyvalue.cs
摘自:http://www.atmarkit.co.jp/fdotnet/dotnettips/441sortbyvalue/sortbyvalue.html
using System;
using System.Net;
using System.Text.RegularExpressions;
using System.Collections.Generic;
class DictionarySortByValue {
static void makeDict(Dictionary<string, int> dict) {
string html;
// HTMLの取得
using (WebClient wc = new WebClient()) {
html = wc.DownloadString("http://www.atmarkit.co.jp/");
}
// 単語に分割して、各単語の出現頻度をカウント
foreach (string s in Regex.Split(html, "\\W")) {
string word = s.Trim();
if (word != "") {
if (dict.ContainsKey(word)) {
dict[word]++;
} else {
dict[word] = 1;
}
}
}
}
static void Main() {
Dictionary<string, int> dict = new Dictionary<string, int>();
makeDict(dict);
List<KeyValuePair<string, int>> sorted = sortByValue(dict);
// sorted.Reverse(); // 逆順にする場合
foreach (KeyValuePair<string, int> kvp in sorted) {
Console.WriteLine(kvp.Key + ":" + kvp.Value);
}
// 出力例:
// a:542
// td:394
// 0:328
// width:289
// tr:284
// ……
}
static List<KeyValuePair<string, int>>
sortByValue(Dictionary<string, int> dict)
{
List<KeyValuePair<string, int>> list
= new List<KeyValuePair<string, int>>(dict);
// Valueの大きい順にソート
list.Sort(
delegate(KeyValuePair<string, int> kvp1, KeyValuePair<string, int> kvp2) {
return kvp2.Value - kvp1.Value;
});
return list;
}
}
// コンパイル方法:csc sortbyvalue.cs
![]() | 本作品采用 知识共享署名-非商业性使用 2.5 中国大陆许可协议进行许可。 |