0

I've got a windows form application in c# which I'm displaying a dictionary.

What I want to do is to sort the dictionary.

Within the dictionary there are file paths along with a value of how many times there are in the dictionary. I want to sort the list to have the entry that appears the most at the top and the lowest at the bottom

I've got no idea where to start.

This is my code at the moment:

    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);
        }
Chad Nouis
  • 6,861
  • 1
  • 27
  • 28
Fraser Munro
  • 21
  • 1
  • 9

1 Answers1

2

You may try this:

List<KeyValuePair<string, int>> myList = _dictionary.ToList();

myList.Sort((firstPair,nextPair) =>
    {
        return firstPair.Value.CompareTo(nextPair.Value);
    }
);

Let's do some testing. With this code:

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

I get:

57 Pouetr
33 Toto
22 Tutu
2 Pouet

So, technically, it's working...

Thomas Ayoub
  • 29,063
  • 15
  • 95
  • 142
  • This appears to only show entries with only one entry i've got the code like this: 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.Value))); } – Fraser Munro Sep 21 '15 at 13:30
  • @FraserMunro please edit your question when you want to add code – Thomas Ayoub Sep 21 '15 at 13:56
  • With this answer you gave @Thomas the display is only values of one – Fraser Munro Sep 21 '15 at 13:58
  • @FraserMunro it works well on mine... Maybe you should add a dump of the dictionnary to see what's wrong ? – Thomas Ayoub Sep 21 '15 at 14:10