Note that there are some explanatory texts on larger screens.

plurals
  1. POC#: How to order arrays in a predefined custom order by only the first 4 digits?
    text
    copied!<p>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. </p> <p>For example the user would paste this into the box:</p> <pre><code>part # QTY CS01-111-111 3 CS02-222-222 3 CS03-333-111 3 CS03-333-333 3 </code></pre> <p>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:</p> <pre><code>part # QTY CS03-333-111 3 CS03-333-333 3 CS01-111-111 3 CS02-222-222 3 </code></pre> <p>I have some C# code to help me do this, but it keeps locking up. </p> <pre><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 WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } public class Comparer : IComparer&lt;string&gt; { private Dictionary&lt;string, int&gt; _order; public Comparer() { _order = new Dictionary&lt;string, int&gt;(); _order.Add("CS01", 1); _order.Add("CS58", 2); _order.Add("CS11", 3); } public int Compare(string x, string y) { if (x.Length &lt; 4 || y.Length &lt; 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&lt;string&gt;(items, 0, items.Length, new Comparer()); textBox1.Text = String.Join(Environment.NewLine, items); } } } </code></pre> <p>Any ideas what I can do to fix it?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload