I want to create a simple C# GUI with a textbox in it for users to paste content into and re-copy the sorted content out.
For example the user would paste this into the box:
part # QTY
CS01-111-111 3
CS02-222-222 3
CS03-333-111 3
CS03-333-333 3
Then I want the program to sort anything pasted into it like this. Sorting by the first 4 digits only, but retaining the QTY value after it:
part # QTY
CS03-333-111 3
CS03-333-333 3
CS01-111-111 3
CS02-222-222 3
I have some C# code to help me do this, but it keeps locking up.
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();
}
public class Comparer : IComparer<string>
{
private Dictionary<string, int> _order;
public Comparer()
{
_order = new Dictionary<string, int>();
_order.Add("CS01", 1);
_order.Add("CS58", 2);
_order.Add("CS11", 3);
}
public int Compare(string x, string y)
{
if (x.Length < 4 || y.Length < 4)
return x.CompareTo(y);
if (!_order.ContainsKey(x.Substring(0, 4)) || !_order.ContainsKey(y.Substring(0, 4)))
return x.CompareTo(y);
return _order[x.Substring(0, 4)].CompareTo(_order[y.Substring(0, 4)]);
}
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
string[] items = textBox1.Text.Split(Environment.NewLine.ToCharArray());
Array.Sort<string>(items, 0, items.Length, new Comparer());
textBox1.Text = String.Join(Environment.NewLine, items);
}
}
}
Any ideas what I can do to fix it?
Here are a couple of modifications to the code I posted previously.
The constructor changes may or may not work for you, but could save you some typing.
This Compare method should be almost twice as fast, because it does half as many Dictionary look-ups.