views:

212

answers:

4

I adjusted this more and came up with the following code and I think my only problem is how I instruct it to read from textbox1 and output into textbox2 with the sorting instructions. I've been fighting this for days off and on if someone could help me out, thanks!

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        System.Collections.Generic.Dictionary<string, int> myDict = new Dictionary<string, int>();
        myDict.Add("one", 1);
        myDict.Add("four", 4);
        myDict.Add("two", 2);
        myDict.Add("three", 3);

        var sortedDict = (from entry in myDict orderby entry.Value ascending select entry);

        string[] items = textBox1.Text.Split(Environment.NewLine.ToCharArray());
        Array.Sort(items, new ??? ());  
        textBox2.Text = String.Join(Environment.NewLine, items);




    }

}
}
A: 

It seems like the whole problem could be worked around by simply using the SortedDictionary class instead of a normal dictionary.

Steve Danner
I"m using one, two, three as examples but it will be more involved later.
john
Yeah so? Why is that going to stop you using a sorted dictionary?
Anon.
Oh ok forgive me, I'm still new and have gotten ahead of myself and I just want to wrap up this one task I am helping someone with. Could you show me how that can work with textboxes and my example?
john
He's not asking to sort the Dictionary, he's asking to sort an array of strings based on values by matching the keys in a Dictionary.
pdr
+1  A: 

try this...

    private void button1_Click(object sender, EventArgs e)
    {
        textBox2.Lines = textBox1.Lines.OrderBy(x => x).ToArray();
    }

or with custom sorting

    private void button1_Click(object sender, EventArgs e)
    {
        textBox2.Lines = textBox1.Lines.OrderBy(x => x,new Comparer()).ToArray();
    }
    public class Comparer : IComparer<string>
    {
        public int Compare(string x, string y)
        {
            //logic here
            return 0;
        }
    }
Paul Creasey
+2  A: 

It doesn't matter what order you put things in your dictionary as they will be reordered by the framework. Your Comparer should have a Compare method that looks a little like this

public int Compare(string x, string y)
{
    return _dict[x].Compare(_dict[y]);
}

[Edit] Just saw the last (scroll-down) bit of your code. This should now look like

string[] items = textBox1.Text.Split(Environment.NewLine.ToCharArray());
Array.Sort(items, new Comparer());
textBox2.Text = String.Join(Environment.NewLine, items);
pdr
+1  A: 

When you implement the IComparer interface, you have to add a definition for the Compare method, and that is what the error message you have seen said.

See this example in MSDN.

Sameh Serag