Sorting a dictionary in a C# Windows Form
本问题已经有最佳答案,请猛点这里访问。
我有一个C语言的Windows窗体应用程序,我正在显示一本字典。
我要做的是把字典分类。
在字典中有文件路径以及字典中有多少次的值。我想对列表进行排序,使条目在顶部显示得最多,在底部显示得最少
我不知道从哪里开始。
这是我目前的代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 | namespace Xml_reader { public partial class Form1 : Form { Dictionary<string, int> _dictionary = new Dictionary<string, int>(); public Form1() { InitializeComponent(); } private void getFile_Click(object sender, EventArgs e) { FileStream fileStream = null; fileStream = new FileStream(@"C:\myProject\svn.xml", FileMode.Open, FileAccess.Read, FileShare.Read); XmlSerializer xmlSerializer = new XmlSerializer(typeof(log) ); log logInstance; lock (xmlSerializer) { logInstance = (log)xmlSerializer.Deserialize(fileStream); } _dictionary = CreateDictionary(logInstance); } private Dictionary<string, int> CreateDictionary(log logInstance) { Dictionary<string, int> dictionary = new Dictionary<string, int>(); int commonStringNumber = FindCommonString(logInstance); for (int entryIdex = 0; entryIdex < logInstance.logentry.Count(); entryIdex++) { logLogentry entry = logInstance.logentry[entryIdex]; for (int pathIdex = 0; pathIdex < entry.paths.Count(); pathIdex++) { logLogentryPath path = entry.paths[pathIdex]; string filePath = path.Value; if (filePath.Length >= commonStringNumber) { string cutPath = filePath.Substring(commonStringNumber); if (dictionary.ContainsKey(cutPath)) { dictionary[cutPath]++; } else { dictionary.Add(cutPath, 1); } } } } return dictionary; } private static int FindCommonString(log logInstance) { string fCompare = logInstance.logentry[0].paths[0].Value; for (int entryIdex = 0; entryIdex < logInstance.logentry.Count(); entryIdex++) { logLogentry entry = logInstance.logentry[entryIdex]; for (int pathIdex = 0; pathIdex < entry.paths.Count(); pathIdex++) { logLogentryPath path = entry.paths[pathIdex]; string fcheck = path.Value; fCompare = similarString(fCompare, fcheck); } } return fCompare.Length; } private static string similarString(string fCompare, string fcheck) { int length = Math.Min(fCompare.Length, fcheck.Length); string common = String.Empty; for (int i = 0; i < length; i++) { if (fCompare[i] == fcheck[i]) { common += fCompare[i]; } else { break; } } return common; } private void converToText(Dictionary<string, int> dictionaryList) { List<KeyValuePair<string, int>> changesWithValues = dictionaryList.ToList(); display(changesWithValues); } private void display(List<KeyValuePair<string, int>> changesWithValues) { textBox1.Text = String.Join(Environment.NewLine, changesWithValues.Where(kvp => kvp.Key !="").Select(kvp => string.Format("{0} = {1}", kvp.Key, kvp.Value))); } private void Show_Click(object sender, System.EventArgs e) { converToText(_dictionary); } |
你可以试试这个
1 2 3 4 5 6 7 | List<KeyValuePair<string, int>> myList = _dictionary.ToList(); myList.Sort((firstPair,nextPair) => { return firstPair.Value.CompareTo(nextPair.Value); } ); |
让我们做些测试With this code:
ZZU1
我得到:
1 2 3 4 | 57 Pouetr 33 Toto 22 Tutu 2 Pouet |
所以,技术上,它在工作…