Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/293.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.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#矩形数组排序 string[,]desc=新字符串[255,10]; int descLines=0; cont string RDATAPATCH=“rctdata.xdb”; 使用(StreamReader sr=新StreamReader(RDATAPATCH)) { descLines=0; while(sr.Peek()>-1) { sr.ReadLine(); descLines++; } desc=新字符串[descLines,10]; 内线=0; sr.BaseStream.Position=0; sr.BufferedData(); while(sr.Peek()>-1) { 字符串ltxt=sr.ReadLine(); 字符串[]lstxt=ltxt.Split(“|”); 对于(int x=0;x,而不是 String [],。你不必读文件两次;排序你的列表会更容易;你的算法问题可能会消失。_C#_String_Sorting_Multidimensional Array - Fatal编程技术网

C#矩形数组排序 string[,]desc=新字符串[255,10]; int descLines=0; cont string RDATAPATCH=“rctdata.xdb”; 使用(StreamReader sr=新StreamReader(RDATAPATCH)) { descLines=0; while(sr.Peek()>-1) { sr.ReadLine(); descLines++; } desc=新字符串[descLines,10]; 内线=0; sr.BaseStream.Position=0; sr.BufferedData(); while(sr.Peek()>-1) { 字符串ltxt=sr.ReadLine(); 字符串[]lstxt=ltxt.Split(“|”); 对于(int x=0;x,而不是 String [],。你不必读文件两次;排序你的列表会更容易;你的算法问题可能会消失。

C#矩形数组排序 string[,]desc=新字符串[255,10]; int descLines=0; cont string RDATAPATCH=“rctdata.xdb”; 使用(StreamReader sr=新StreamReader(RDATAPATCH)) { descLines=0; while(sr.Peek()>-1) { sr.ReadLine(); descLines++; } desc=新字符串[descLines,10]; 内线=0; sr.BaseStream.Position=0; sr.BufferedData(); while(sr.Peek()>-1) { 字符串ltxt=sr.ReadLine(); 字符串[]lstxt=ltxt.Split(“|”); 对于(int x=0;x,而不是 String [],。你不必读文件两次;排序你的列表会更容易;你的算法问题可能会消失。,c#,string,sorting,multidimensional-array,C#,String,Sorting,Multidimensional Array,我有以下代码,streamreader加载的文件如下: 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) {

我有以下代码,streamreader加载的文件如下:

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;
id | rid | type |打开日期| code | |打开日期|可用性('in stoc'或'11.11.2010')>|
0 | 0 | 15fl*20ml/切割| 2012年4月| 200905.101109 | 1 | 1 | nedeschis |在stoc | 2
1 | 0 | 15fl*20ml/切| 07.2012 | 200905.030210 | 1 | 1 | nedeschis |在stoc | 2
2 | 10 | 150测试/切割| 2012年11月| 16813A | 1 | 3 | nedeschis |在stoc | 2
3 | 0 | 15fl*20ml/切割| 06.2011 | 200905.050309 | 0 | 11 | 07.07.2010 |在stoc | 0
desc变量按打开日期字符串排序,可以是:“nedeschis”(关闭)或“11.11.2010”(日期)。
我认为我的算法是错误的,有人能帮助我吗?< /P> < P>我不能从问题的角度来判断,但是考虑切换到<代码>列表>代码>,而不是<代码> String [],。你不必读文件两次;排序你的列表会更容易;你的算法问题可能会消失。
在.NET中,像
string[,]
这样的多维数组使用起来相当痛苦,因为大多数方法只适用于一维数组。您可以使用锯齿数组(字符串[])模拟二维数组。但是,在您的情况下,
列表将是一个更好的选择,因为您事先不知道大小。

该算法似乎基本正确,但是由于值是按字符串排序的,因此结果不会按时间顺序排列。例如,字符串值“07.07.2010”大于“06.08.2010”。您必须将值转换为日期时间值,以便正确比较它们

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

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

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
List desc=new List();
常量字符串RDATAPATCH=“rctdata.xdb”;
使用(StreamReader sr=新StreamReader(RDATAPATCH)){
弦线;
而((line=sr.ReadLine())!=null){
描述添加(行分割(“|”);
}
}
desc.RemoveAt(0);//删除字段描述行
描述排序((a,b)=>{
如果(a[7]=“nedeschis”&&b[7]=“nedeschis”)返回0;
如果(a[7]=“nedeschis”)返回-1;
如果(b[7]=“nedeschis”)返回1;
return DateTime.Parse(a[7]).CompareTo(DateTime.Parse(b[7]);
});

我在这方面是新手,你能给我一个演示吗…我在项目中做得更深入了一点,这个改变可以让我修改很多代码…我可以给你所有的项目来了解我的意思…如果你有时间帮我:D…谢谢你的快速回复…你能给我一个雅虎id与你聊天吗?再次感谢。很抱歉我的错误英语请看一下@Guffa的答案,作为一个例子
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]));
});