c# - 如何将列表中的项目排列成系列排列

标签 c# list

我有一个数据列表,其中包含随机数据以及字符串和数字的组合:

List<String> Data1 = new List<String>()
{
  "1001A",
  "1002A",
  "1003A",
  "1004A",
  "1015A",
  "1016A",
  "1007A",
  "1008A",
  "1009A",
};

我希望这些数据像这样排列成系列:

1001A - 1004A, 1007A - 1009A, 1015A, 1016A

对于每超过 2 个计数的数据系列,输出应在系列的第一个计数和最后一个计数之间有“-”,其他非系列数据将被添加到最后一部分,所有一起将被分隔“,”。

我已经编写了一些代码,只是按照它的最后一个字符排列数据系列:

string get_REVISIONMARK = "A";

var raw_serries = arrange_REVISIONSERIES.Where(p => p[p.Length -       1].ToString() == get_REVISIONMARK) .OrderBy(p => p[p.Length - 1) .ThenBy(p => p.Substring(0, p.Length - 1)).ToList();

just ignore the last char I'd already have function for that, and my problem only about the arrangement of the numbers, the length of data is not fixed. for other example of output "1001A - 1005A, 301A, 32A"

I had another sample of my codes this works fine to me, but for me its so lazy code.

for (int c1 = 0; c1 < list_num.Count; c1++)
{
    if (list_num[c1] != 0)
    {
         check1 = list_num[c1];
         for (int c2 = 0; c2 < list_num.Count; c2++)
         {
              if (check1 == list_num[c2])
              {
                  list_num[c2] = 0;
                  check1 += 1;
                  list_series.Add(arrange_REVISIONSERIES[c2]);
              }
         }
         check1 = 0;
         if (list_series.Count > 2)
         {
             res_series.Add(list_series[0] + " to " +list_series[list_series.Count - 1]);
             list_series.Clear();
         }
         else
         {
             if (list_series.Count == 1)
             {
                 res_series.Add(list_series[0]);
                 list_series.Clear();
             }
             else
             {
                 res_series.Add(list_series[0] + "," + list_series[1]);
                 list_series.Clear();
             }
        }
    }
}
var combine_res = String.Join(",", res_series);
MessageBox.Show(combine_res);

此代码适用于序列号 ...

最佳答案

可能的解决方案(使用当前值集),请按照以下步骤操作

声明一个类级别的字符串列表为

    public List<String> data_result = new List<string>();

创建一个函数来遍历输入字符串列表(输入字符串在内部声明,名为'data')

    public void ArrangeList()
    {
        List<String> data = new List<string>() { "1001A", "1002A", "1003A", 
        "1004A", "1015A", "1016A", "1007A", "1008A", "1009A", "1017A" };
        List<int> data_int = data.Select(a => Convert.ToInt32(a.Substring(0,
        a.Length - 1))).OrderBy(b => b).ToList();
        int initializer = 0, counter = 0;
        int finalizer = 0;

        foreach (var item in data_int)
        {
            if (initializer == 0)
            { initializer = item; continue; }
            else
            {
                counter++;
                if (item == initializer + counter)
                    finalizer = item;
                else
                {
                    LogListing(initializer, finalizer);
                    initializer = item;
                    finalizer = item;
                    counter = 0;
                }
            }
        }
        LogListing(initializer, finalizer);
    }

创建一个将结果记录到 data_result 字符串列表中的函数。

    public void LogListing(int initializer, int finalizer)
    {
        if (initializer != finalizer)
        {
            if (finalizer == initializer + 1)
            {
                data_result.Add(initializer + "A");
                data_result.Add(finalizer + "A");
            }
            else
                data_result.Add(initializer + "A - " + finalizer + "A");

        }
        else
            data_result.Add(initializer + "A");
    }


它完美地生成结果列表为
enter image description here

喜欢就点个赞吧

关于c# - 如何将列表中的项目排列成系列排列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32305658/

相关文章:

c# - 找不到程序集

c# - wpf web 浏览器控件的限制是什么?

Python - 列表转换

list - 反转列表列表

c# - 在 ASMX Web 服务之间共享枚举

c# - 共享对象和线程

C++ 对链表进行排序的最佳方法?

c - 如何使用 for() 表达式枚举循环链表?

c# - 如何在 visual studio 2013 中设置 "Target Framework"?

Python根据列表中的日期获取记录