Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#:如何按预定义的自定义顺序仅按前4位对数组进行排序?_C#_Sorting - Fatal编程技术网

C#:如何按预定义的自定义顺序仅按前4位对数组进行排序?

C#:如何按预定义的自定义顺序仅按前4位对数组进行排序?,c#,sorting,C#,Sorting,我想创建一个简单的C#GUI,其中包含一个文本框,供用户将内容粘贴到其中并重新复制已排序的内容 例如,用户会将其粘贴到框中: part # QTY CS01-111-111 3 CS02-222-222 3 CS03-333-111 3 CS03-333-333 3 然后我希望程序对粘贴到其中的任何内容进行排序,如下所示。仅按前4位排序,但保留后面的数量值: part # QTY CS03-333-111 3 CS03-333-333 3 CS01-1

我想创建一个简单的C#GUI,其中包含一个文本框,供用户将内容粘贴到其中并重新复制已排序的内容

例如,用户会将其粘贴到框中:

part #        QTY
CS01-111-111  3
CS02-222-222  3
CS03-333-111  3
CS03-333-333  3 
然后我希望程序对粘贴到其中的任何内容进行排序,如下所示。仅按前4位排序,但保留后面的数量值:

part #        QTY
CS03-333-111  3
CS03-333-333  3
CS01-111-111  3
CS02-222-222  3
我有一些C#代码来帮助我做到这一点,但它一直锁定

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);
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.Linq;
使用系统文本;
使用System.Windows.Forms;
命名空间Windows窗体应用程序1
{
公共部分类Form1:Form
{
公共表格1()
{
初始化组件();
}
公共类比较器:IComparer
{
私人字典(顺序),;
公共比较器()
{
_order=新字典();
_订单。添加(“CS01”,1);
_添加命令(“CS58”,2);
_订单。添加(“CS11”,3);
}
公共整数比较(字符串x、字符串y)
{
如果(x.长度<4 | | y.长度<4)
返回x.CompareTo(y);
if(!_顺序.ContainsKey(x.子字符串(0,4))| |!_顺序.ContainsKey(y.子字符串(0,4)))
返回x.CompareTo(y);
返回_顺序[x.子字符串(0,4)]。比较(_顺序[y.子字符串(0,4)]);
}
}
私有void textBox1\u TextChanged(对象发送方,事件参数e)
{
string[]items=textBox1.Text.Split(Environment.NewLine.ToCharArray());
Sort(items,0,items.Length,new Comparer());
textBox1.Text=String.Join(Environment.NewLine,items);
}
}
}

想知道我能做些什么来修复它吗?

创建一个比较器,其中包含一个字典,用于确定字符串的排序方式:

public class Comparer : IComparer<string> {

   private Dictionary<string, int> _order;

   public Comparer() {
      _order = new Dictionary<string, int>();
      _order.Add("03-33", 1);
      _order.Add("01-11", 2);
      _order.Add("02-22", 3);
   }

   public int Compare(string x, string y) {
      return _order[x.Substring(2, 5)].CompareTo(_order[y.Substring(2, 5)]);
   }

}

创建一个比较器,该比较器包含一个字典,用于确定字符串的排序方式:

public class Comparer : IComparer<string> {

   private Dictionary<string, int> _order;

   public Comparer() {
      _order = new Dictionary<string, int>();
      _order.Add("03-33", 1);
      _order.Add("01-11", 2);
      _order.Add("02-22", 3);
   }

   public int Compare(string x, string y) {
      return _order[x.Substring(2, 5)].CompareTo(_order[y.Substring(2, 5)]);
   }

}

它被锁定是因为您正在更改textBox1\u TextChanged方法中的textBox1.Text。因此,每次textBox1_TextChanged完成时,都会使用新的文本值再次调用它

你需要这样的东西:

private bool _updating = false;
private void textBox1_TextChanged(object sender, EventArgs e)
{
    if (!_updating)
    {
        try
        {
            _updating = true;

            // do work ...

            textBox1.Text = ...
        }
        finally
        {
            _updating = false;
        }            
    }
}
或者,您可以在更改textBox1之前检查该值。文本:

string newValue = ... ;
if (textBox1.Text != newValue)
{
    textBox1.Text = newValue;
}

它被锁定是因为您正在更改textBox1\u TextChanged方法中的textBox1.Text。因此,每次textBox1_TextChanged完成时,都会使用新的文本值再次调用它

你需要这样的东西:

private bool _updating = false;
private void textBox1_TextChanged(object sender, EventArgs e)
{
    if (!_updating)
    {
        try
        {
            _updating = true;

            // do work ...

            textBox1.Text = ...
        }
        finally
        {
            _updating = false;
        }            
    }
}
或者,您可以在更改textBox1之前检查该值。文本:

string newValue = ... ;
if (textBox1.Text != newValue)
{
    textBox1.Text = newValue;
}

下面是对我之前发布的代码的一些修改

构造函数的更改可能对您有效,也可能对您无效,但可以为您节省一些输入

这种比较方法的速度应该是原来的两倍,因为它查找字典的次数是原来的一半

public class Comparer : IComparer<string>
{

    private Dictionary<string, int> _order;

    public Comparer()
    {
        List<string> list = new List<string>()
        {
            "CS01",
            "CS58",
            "CS11"
        };

        _order = new Dictionary<string, int>();
        for (int i = 0; i < list.Count; i++)
        {
            _order.Add(list[i], i);
        }
    }

    public int Compare(string x, string y)
    {
        if (x.Length < 4 || y.Length < 4)
            return x.CompareTo(y);

        string xPrefix = x.Substring(0, 4);
        string yPrefix = y.Substring(0, 4);

        int xSequence;
        int ySequence;
        if (_order.TryGetValue(xPrefix, out xSequence)
            && _order.TryGetValue(yPrefix, out ySequence))
        {
            return xSequence.CompareTo(ySequence);
        }
        else
        {
            return x.CompareTo(y);
        }
    }
}
公共类比较器:IComparer
{
私人字典(顺序),;
公共比较器()
{
列表=新列表()
{
“CS01”,
“CS58”,
“CS11”
};
_order=新字典();
for(int i=0;i
以下是对我之前发布的代码的一些修改

构造函数的更改可能对您有效,也可能对您无效,但可以为您节省一些输入

这种比较方法的速度应该是原来的两倍,因为它查找字典的次数是原来的一半

public class Comparer : IComparer<string>
{

    private Dictionary<string, int> _order;

    public Comparer()
    {
        List<string> list = new List<string>()
        {
            "CS01",
            "CS58",
            "CS11"
        };

        _order = new Dictionary<string, int>();
        for (int i = 0; i < list.Count; i++)
        {
            _order.Add(list[i], i);
        }
    }

    public int Compare(string x, string y)
    {
        if (x.Length < 4 || y.Length < 4)
            return x.CompareTo(y);

        string xPrefix = x.Substring(0, 4);
        string yPrefix = y.Substring(0, 4);

        int xSequence;
        int ySequence;
        if (_order.TryGetValue(xPrefix, out xSequence)
            && _order.TryGetValue(yPrefix, out ySequence))
        {
            return xSequence.CompareTo(ySequence);
        }
        else
        {
            return x.CompareTo(y);
        }
    }
}
公共类比较器:IComparer
{
私人字典(顺序),;
公共比较器()
{
列表=新列表()
{
“CS01”,
“CS58”,
“CS11”
};
_order=新字典();
for(int i=0;i
请发布您目前编写的代码。人们通常不喜欢只为您编写代码。02-22不应该在01-11之前出现吗?请发布到目前为止您编写的代码。人们通常不喜欢只为您编写代码。02-22不应该在01-11之前吗?字典是必要的吗?为什么没有比较器就不能创建比较器?@Corey:这不是必需的,当然可以用多种不同的方式将字符串映射到排序值。字典很容易添加更多的字符串,它有一个非常快速的查找和它的规模非常好。字典是必要的吗?为什么没有比较器就不能创建比较器?@Corey:这不是必需的,当然可以用多种不同的方式将字符串映射到排序值。字典很容易添加更多的字符串,它有一个非常快速的查找和它的规模非常好。