【问题标题】:Sorting a dictionary in a C# Windows Form [duplicate]在 C# Windows 窗体中对字典进行排序[重复]
【发布时间】:2015-12-18 05:08:54
【问题描述】:

我在 c# 中有一个 Windows 窗体应用程序,我正在显示字典。

我要做的是对字典进行排序。

在字典中有文件路径以及字典中有多少次的值。我想对列表进行排序以使出现最多的条目出现在顶部,而出现在底部的条目最少

我不知道从哪里开始。

这是我目前的代码:

    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);
        }

【问题讨论】:

    标签: c# sorting dictionary


    【解决方案1】:

    你可以试试这个:

    List<KeyValuePair<string, int>> myList = _dictionary.ToList();
    
    myList.Sort((firstPair,nextPair) =>
        {
            return firstPair.Value.CompareTo(nextPair.Value);
        }
    );
    

    让我们做一些测试。使用此代码:

    _dictionary.Add("Toto", 33);
    _dictionary.Add("Tutu", 22);
    _dictionary.Add("Pouet", 2);
    _dictionary.Add("Pouetr", 57);
    
    
    List<KeyValuePair<string, int>> myList = _dictionary.ToList();
    
    myList.Sort((firstPair, nextPair) => firstPair.Value.CompareTo(nextPair.Value));
    myList.Reverse();
    
    foreach (KeyValuePair<string, int> keyValuePair in myList)
    {
        Console.Out.WriteLine(keyValuePair.Value + " " + keyValuePair.Key);
    }
    

    我明白了:

    57 Pouetr
    33 Toto
    22 Tutu
    2 Pouet
    

    所以,从技术上讲,它正在工作......

    【讨论】:

    • 这似乎只显示只有一个条目的条目我有这样的代码: private void display(List> changesWithValues) { List> myList = changesWithValues.ToList(); myList.Sort((firstPair, nextPair) => { return firstPair.Value.CompareTo(nextPair.Value); } ); textBox1.Text = String.Join(Environment.NewLine, myList.Where(kvp => kvp.Key != "").Select(kvp => string.Format("{0} = {1}", kvp.Key , kvp.值))); }
    • @FraserMunro 请在您想添加代码时编辑您的问题
    • 有了这个答案,你给了@Thomas,显示的只是一个值
    • @FraserMunro 它适用于我的...也许你应该添加一个字典转储看看有什么问题?
    猜你喜欢
    • 2010-10-22
    • 2021-02-17
    • 2015-12-26
    • 2010-10-09
    • 2022-01-19
    • 1970-01-01
    • 2020-11-10
    相关资源
    最近更新 更多