I want to do this with a simple text box GUI for folks to paste into and re-copy. For example if it started like this:
Before
part # QTY CS01-111-111 3 CS02-222-222 3 CS03-333-111 3 CS03-333-333 3
I'd like textBox1.text to sort anything pasted into it like this going by the first 4 digits only, but retaining the QTY value after it:
After:
part # QTY CS03-333-111 3 CS03-333-333 3 CS01-111-111 3 CS02-222-222 3
It has no numerical order or pattern, it is just the way it is needed, so I would have to specify all the 4 digit numbers and their order.
I only have about 20 4 digit numbers to code, so it will not be a problem to do this manually after I get a start. Thank you very much for reading.
UPDATE:
Ok I have used Guffa's solution (thanks Guffa!!) to help me come up with the below, but it keeps locking up. Any ideas what I can do? 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();
}
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);
}
}
}