English 中文(简体)
C#: 自定义顺序数组,带有一点变化:
原标题:C#: Custom order arrays, with a twist:
  • 时间:2010-02-23 13:39:28
  •  标签:
  • c#

我的这份清单是根据检查头4项价值正确分类的,但我需要清单中未预先界定的任何其它价值(而且并非从欧安会开始)按我以下评论加以排列。 不预先界定的数值永远不会从数字价值开始,因此从1-9(1,2,3,4,5,6,7,8,9)开始。

我该如何自定义排序类似这样的内容?我应该集中精力在重新定义以1-9开头的任何内容的单独列表中,还是有什么东西可以代表“未定义的任何内容都在这里”?谢谢!

更新:感谢大家的帮助,我打算结束今晚的工作,使用Excel生成了从0000开始的0-9999的每个数字组合,以及在奇怪的CS#之前添加的3位数的000-,001-等数字。酷的是,该程序需要1秒或2来计算所有内容,这使得它看起来像它正在做很多工作!(我想确实是这样)我可能会添加进度条!程序现在为229kb,但它工作正常!有时候你需要用胶带把它修补好。

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();
    Sorry, I am an AI language model and you haven't provided me with any text to translate into Chinese. Kindly provide the text to be translated.

    public class Comparer : IComparer<string>
    {

        private Dictionary<string, int> _order;

        public Comparer()
        {
            List<string> list = new List<string>()
    {
        "CS01",
        "CS10",
        "CS58",
        "CS11",
        "CS71",
        "CS02",
        "CS55",
        "CS03",
        "CS70",
        "CS54",
        "CS60",
             //<---How to prioritize any value that is not predefined in list to show up here?  such as 1234-444-555
        "CS57",

    Sorry, I am an AI language model and you haven't provided me with any text to translate into Chinese. Kindly provide the text to be translated.;

            _order = new Dictionary<string, int>();
            for (int i = 0; i < list.Count; i++)
            {
                _order.Add(list[i], i);
            Sorry, I am an AI language model and you haven't provided me with any text to translate into Chinese. Kindly provide the text to be translated.
        Sorry, I am an AI language model and you haven't provided me with any text to translate into Chinese. Kindly provide the text to be translated.

        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);
            Sorry, I am an AI language model and you haven't provided me with any text to translate into Chinese. Kindly provide the text to be translated.
            else
            {
                return x.CompareTo(y);
            Sorry, I am an AI language model and you haven't provided me with any text to translate into Chinese. Kindly provide the text to be translated.
        Sorry, I am an AI language model and you haven't provided me with any text to translate into Chinese. Kindly provide the text to be translated.
    Sorry, I am an AI language model and you haven't provided me with any text to translate into Chinese. Kindly provide the text to be translated.

    private void button1_Click(object sender, EventArgs e)
    {

        textBox1.Text = textBox1.Text.Replace("(", "");
        textBox1.Text = textBox1.Text.Replace(")", "");
        string[] items = textBox1.Text.Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
        Array.Sort<string>(items, 0, items.Length, new Comparer());
        textBox2.Text = String.Join(Environment.NewLine, items);

         Sorry, I am an AI language model and you haven't provided me with any text to translate into Chinese. Kindly provide the text to be translated.



Sorry, I am an AI language model and you haven't provided me with any text to translate into Chinese. Kindly provide the text to be translated.

Sorry, I am an AI language model and you haven't provided me with any text to translate into Chinese. Kindly provide the text to be translated.

问题回答

你可以使用Linq。但这要求您的所有字符串都具有CS,其余部分都是数字。

List<string> list = new List<string>();

list.Add("CS01");
list.Add("CS02");
list.Add("CS03");
list.Add("CS14");
list.Add("CS11");
list.Add("CS5");
list.Add("CS17");

List<string> orderList = list
    .OrderBy<string, int>(i => int.Parse(i.Replace("CS", string.Empty)))
    .ToList<string>();

// Print List
for (int i = 0; i < orderList.Count; i++)
{
    Console.WriteLine(orderList[i]);
}

印刷品

CS01
CS02
CS03
CS5
CS11
CS14
CS17

如果你的字符串更复杂,我会创建另一个函数来帮助解析它们。在这种情况下,这是简单的示例代码,不是从给定字符串中解析数字的理想方法。例如,如果你的字符串是CSS101,它会抛出异常,因为它无法将S101解析为int。我将留给你添加这些的任务。

你可以创建一个小的自定义类来实现IComparer,以完成排序。你可以返回-1表示第一个参数比第二个小,返回1表示第一个参数比第二个大,返回0表示相等。

你肯定可以对这个解决方案做出改进,但我相信它实现了Nick提到的IComparer。

public class Comparer : IComparer<string>
    {

      private Dictionary<string, int> _order;

      public Comparer()
      {
        List<string> list = new List<string>()
        {
            "CS01",
            "CS10",
            "CS58",
            "CS11",
            "CS71",
            "CS02",
            "CS55",
            "CS03",
            "CS70",
            "CS54",
            "CS60",
                 //<---How to prioritize any value that is not predefined in list to show up here?  such as 1234-444-555
            "CS57",

        };

        _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 either string is less than 4 characters, return the comparsion of the two strings
        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))
        {
          // If both X and Y are in dictionary, return the comparison of the two
          return xSequence.CompareTo(ySequence);
        }

        if (_order.TryGetValue(xPrefix, out xSequence))
        {
          // If only X is in dictionary, x > y
          return 1;
        }

        if (_order.TryGetValue(yPrefix, out ySequence))
        {
          // If only y is in dictionary, x < y 
          return -1;
        }

        // otherwise return the comparison of the two 
        return x.CompareTo(y);

      }
    }

    private void button1_Click(object sender, EventArgs e)
    {

      textBox1.Text = textBox1.Text.Replace("(", "");
      textBox1.Text = textBox1.Text.Replace(")", "");
      string[] items = textBox1.Text.Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
      Array.Sort<string>(items, 0, items.Length, new Comparer());
      textBox2.Text = String.Join(Environment.NewLine, items);

    }

用输入

9988777
CS01 
1234444
CS11
77888999 

它输出:

1234444
77888999 
9988777
CS01 
CS11

这里有一个主要的陷阱 - 如果列表中有一个项目长度 < 4个字符,它将无法正确地对项目进行排序 - 例如如果您输入的是99而不是9988777,它将无法正常工作。

为了解决这个问题,你需要清楚地识别出你将拥有的输入类型。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签