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 { { "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 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. } } } |
Sam HobbsPosted Mar 1, 2010, 9:40 PM
I really think you are doing it the long way. Attached is a project that has a form version of the console program. I suspect that the sample you are using is not appropraite for you. The sample shows how to sort a Dictionary by the value, which is very much not normal. The sample is showing how to do it since it is not normal; it is showing something that some people might not know how to do. I think that the primary goal you want is to sort by part number. The only trick is that you should reformat the part number. I really think it is unnecessary to do the Comparer and all that.
The sample I provided shows the result of using the Dictionary in the manner that the sample does. It also shows how to use a SortedDictionary to retrieve items sorted by the key, and for that sample, I use the number as the key.
lisaPosted Mar 2, 2010, 1:35 PM
lisaPosted Mar 1, 2010, 9:25 PM
CODE:
theLizardPosted Mar 1, 2010, 8:25 PM
Would it be helpful to put your part #'s into an SQL table in any order then ask the SQL engine to return the list in the order that you seem to want.
which is what I think you are after.
Ie
ab43321
comes before ae43321 and ae43221 comes before ae43321 and ab31221 comes before ab43321 and ad00110 comes after any of the above.
Is this what you are trying to achieve?
Something else you could consider is a "bubble sort" this algorithm has been around for as long as I have, and that is a long time, search for it on google, wiki shows some delphi examples.
Sam HobbsPosted Mar 1, 2010, 7:55 PM
Does that help?
lisaPosted Mar 1, 2010, 6:25 PM
The best way to explain this, is that I took this whole thing from a console program and did my best to convert it. The console program is like this. (and I am trying to make it see what is in textbox1, and sort it into textbox2)
The only tricky part is in the end I would like to have the ability to sort number sequences by what they start with or end with so I could group all the numbers that start with AB33 and group them together, with AC32 right after but stick in numbers that end with -43 below that, and then go back to sorting by the first 4 letters again etc. I read about doing something similiar with the "startswith" and "endswith" string methods...I don't know if this is going too far for a basic textbox GUI or not though, but it would be really great to be able to do this.
Example:
SORT BY ORDER:
Beginswith ab33 (1st sorting priority)
beginswith ac32 (2nd sorting priority)
endswith -43 (3rd sorting priority)
beginswith ae33(4th, and so on)
I dont mean to jump back and forth between sorting goals but I thought it would make sense to figure out the simpler sort first but if I am doing it by different methods it is a waste of time since being able to sort by what item # begin with and end with simultaneously in the same sort is the goal to really save time in sorting part #'s.
Thanks
Sam HobbsPosted Mar 1, 2010, 5:32 PM
lisaPosted Mar 1, 2010, 2:30 PM
AB343-222-3456
AB233-353-5663
AB335-553-4355
I will have to paste these in from a bill of materials that are in a random order, and then sort them based on their predefined dictionary order (not numerical order of any type). So in my example I tried to show that even if they were already in numerical order, I want to be able to say 132 not 123. I am trying to define in this program by putting 1,2,3,4 next to them within the code, but however they are sorted is fine by me, I am just not able to make it work in any way. I apologize for the clarity issues, I realize how difficult I sound. Thanks again
Sam HobbsPosted Mar 1, 2010, 1:48 PM
1
3
2
That is what you said. I don't understand why you need that, or more importantly, how it is determined that the numbers be put in that order. Perhaps you posted that by mistake; perhaps that is not correct, and I was asking in case it was a mistake.
Note that you posted that example that has just numbers. Then I asked for clarification then you gave an example that has both numbers and text. So please let's stick with one set of sample data.
lisaPosted Mar 1, 2010, 9:37 AM
If it was
{ "one", 2 },
{ "two", 1 },
Textbox 2 would output
two
one
no matter what order I put in one two, or two one in textbox 1 right?
Thanks
Sam HobbsPosted Mar 1, 2010, 2:58 AM
lisaPosted Feb 28, 2010, 2:23 PM
I'm not trying to sort in dictionary/alphabetical/numerical order, but I'm trying to sort an array of strings based on values by matching the keys in a Dictionary, does this change your suggestion?
I'm very very new to c# so I think that my question is so simple it is confusing and I do not word it clearly. I want to be able to paste numbers into box1 and output them sorted into box 2, such as (where 1,2,3 are going to be part #'s later):
textBox1.Text (input)
1
2
3
and have the ability to sort it like this
textBox2.Text (output from textbox1 into textbox2)
1
3
2
I just want to know how to type in the code so that the result appears in textbox2. I may have the code all wrong and that is why it is confusing but I feel that I am close.
Sam HobbsPosted Feb 28, 2010, 1:24 PM