【问题标题】:C# rectangular array sortC#矩形数组排序
【发布时间】:2011-05-16 13:10:05
【问题描述】:
string[,] desc = new string[255,10];    
int descLines = 0;
cont string RDATAPATCH = "rctdata.xdb";
using (StreamReader sr = new StreamReader(RDATAPATCH))
{
      descLines = 0;
      while (sr.Peek() > -1)
      {
         sr.ReadLine();
         descLines++;
      }
      desc = new string[descLines, 10];
      int line = 0;
      sr.BaseStream.Position = 0;
      sr.DiscardBufferedData();
      while (sr.Peek() > -1)
      {
         string ltxt = sr.ReadLine();
         string[] lstxt = ltxt.Split('|');
         for (int x = 0; x < 10; x++)
         {
            desc[line, x] = lstxt[x];
         }
         line++;
      }
}
string[] sArray = new string[descLines];
for (int x = 0; x < descLines; x++)
{
   sArray[x] = desc[x, 7];
}
Array.Sort(sArray);
string[,] tempDesc = new string[descLines, 10];
for (int x = 0; x < sArray.Length; x++)
{
   for (int y = 0; y < desc.Length / 10; y++)
   {
      if (sArray[x] == desc[y, 7])
      {
         for (int z = 0; z < 10; z++)
         {
            tempDesc[x, z] = desc[y, z];
         }
      }
   }
}
desc = tempDesc;

我有这段代码,streamreader 加载的文件是这样的:

id|rid|type|date opened|code|<0/1>|<number>|open date|availability('in stoc' or '11.11.2010'>|<0/1/2>
0|0|15fl*20ml/cut|04.2012|200905.101109|1|1|nedeschis|in stoc|2
1|0|15fl*20ml/cut|07.2012|200905.030210|1|1|nedeschis|in stoc|2
2|10|150 teste/cut|11.2012|16813A|1|3|nedeschis|in stoc|2
3|0|15fl*20ml/cut|06.2011|200905.050309|0|11|07.07.2010|in stoc|0

desc 变量按打开日期字符串排序,可以是:'nedeschis'(关闭)或 '11.11.2010'(日期)。 我认为我的算法是错误的,谁能帮助我?

【问题讨论】:

    标签: c# string sorting multidimensional-array


    【解决方案1】:

    我无法从问题中看出问题所在,但请考虑切换到List&lt;string[]&gt; 而不是string[,]。您不必阅读该文件两次;对列表进行排序会更容易;你的算法问题可能会消失。

    在 .NET 中,像string[,] 这样的多维数组使用起来相当痛苦,因为大多数方法只适用于一维数组。您可以使用锯齿状数组(string[][])模拟二维数组。但是,在您的情况下,List&lt;string[]&gt; 会是更好的选择,因为您事先不知道大小。

    【讨论】:

    • 我是新手,你能给我一个演示吗...我在项目中更进一步,这个更改可以让我修改很多代码...我可以给你所有项目以了解我的意思...如果您有时间帮助我:D...感谢您的快速响应...您可以给我一个雅虎ID与您聊天吗?再次非常感谢。对不起我的英语
    • 以@Guffa 的回答为例
    【解决方案2】:

    算法看起来基本正确,但是由于值是按字符串排序的,结果不会按时间顺序排列。例如,字符串值“07.07.2010”大于“06.08.2010”。您必须将这些值转换为 DateTime 值才能正确比较它们。

    此外,由于您在排序后使用日期值来识别项目,并且值不是唯一的,因此您最终会得到一些项目的重复并丢失其他项目。仍然可以通过这种方式进行排序,但是您必须在排序后删除重复值,并更改匹配值的循环以处理重复匹配项。

    您可以使用字符串数组列表而不是二维数组,这将使代码更简单。您可以一次读取数据,并且可以对列表中的项目进行排序,而不是对特定值进行排序然后匹配项目:

    List<string[]> desc = new List<string[]>();
    const string RDATAPATCH = "rctdata.xdb";
    using (StreamReader sr = new StreamReader(RDATAPATCH)) {
      string line;
      while ((line = sr.ReadLine()) != null) {
        desc.Add(line.Split('|'));
      }
    }
    desc.RemoveAt(0); // remove field description line
    desc.Sort((a, b) => {
      if (a[7] == "nedeschis" && b[7] == "nedeschis") return 0;
      if (a[7] == "nedeschis") return -1;
      if (b[7] == "nedeschis") return 1;
      return DateTime.Parse(a[7]).CompareTo(DateTime.Parse(b[7]));
    });
    

    【讨论】:

      猜你喜欢
      • 2019-01-19
      • 2010-09-18
      • 2013-06-19
      • 2016-03-12
      • 2015-10-17
      • 2014-06-11
      • 1970-01-01
      • 2021-12-26
      相关资源
      最近更新 更多