Tech
Forums
Jobs
Books
Events
Live
Interviews
More
Learn
Training
Career
Members
Videos
News
Blogs
Login
Sign Up
Ask Question
13
Reply
Custom Dictionary Sort C#
lisa
Feb 28 2010 12:32 PM
9k
Reply
Hello,
I am trying to set up a GUI with 2 multi-line textboxes to allow me to paste in part #'s in any order into textBox1.Text, and have the predefined order be spit out and moved into textBox2.Text after I click my button. I am using 1,2,3,4 as examples, but it will be part #'s later and they will have no type of numerical sort order, it is completely random so it has to be a a defined order from a dictionary.
I have googled for this solution and found many answers but all are targeted for console applications and I am not skilled enough yet to restructure a console app to a GUI.
I think that the only issue is the end of the code where I tell it to actually read textbox1 and spit out the re-order into textbox2. I think I should not be using compare which is where i get the error. I think I just need to do a standard array sort which I cannot figure out.
Thank you for taking the time to read this.
code:
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 WindowsFormsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
// You don't have to qualify the whole name. You included the using directive already.
/*System.Collections.Generic.*/
// I changed this to auto-initialize:
Dictionary<string, int> myDict = new Dictionary<string, int>()
{
{ "one", 1 },
{ "four", 4 },
{ "two", 2 },
{ "three", 3 }
};
// OK. Here we are retrieving all items in that dictionary and ordering them by value.
// Value of the dictionary is int. In ascending order, this turns into 1,2,3,4.
var sortedDict = (from entry in myDict orderby entry.Value ascending select entry);
// multi-line textbox
string[] items = textBox1.Text.Split(Environment.NewLine.ToCharArray());
Array.Sort<string>(items, 0, items.Length, new Comparer());
textBox2.Text = String.Join(Environment.NewLine, items);
//This is where I am having the issue, I am having trouble transitioning from reading textbox1 from any order, and spitting it into textBox2.Text in the predefined order. I am not supposed to be using a Comparer I belive, but I am not sure what to do.
}
}
}
Upload Source Code
Select only zip and rar file.
Post
Reset
Cancel
Answers (
13
)
Next Recommended Forum
I want some high cost sql queries and low cost sql quries
where did i mistake?