简体   繁体   中英

C# rectangular array sort

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;

I have this code and the file that streamreader load is like this:

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

the desc variable is sorted by the open date string and can be: 'nedeschis'(closed) or '11.11.2010'(a date). I think that my algorithm is wrong can anyone help me?

I can't tell from the question what the problem is, but consider switching to a List<string[]> instead of a string[,] . You won't have to read the file twice; sorting your list will be easier; and your algorithm problem may go away.

In .NET, multi-dimensional arrays like string[,] are fairly painful to work with, since most methods only work on single-dimensional arrays. You can simulate a 2D array with a jagged array (a string[][] ). However, in your case a List<string[]> would be a better choice, since you don't know the size in advance.

The algorithm seems basically correct, however as the values are sorted as strings, the result will not be in chronological order. For example the string value "07.07.2010" is larger than "06.08.2010". You would have to convert the values to DateTime values in order to compare them correctly.

Also, as you are using the date value to identify the items after sorting, and the values are not unique, you will end up with duplicates of some items and lose other items. It would still be possible to do the sorting that way, but then you would have to remove duplicate values after sorting, and change the loop that matches the values to handle the duplicate matches.

You can use a list of string arrays instead of two dimensional arrays, that would make the code a lot simpler. You can read the data in a single pass, and you can sort the items in the list instead of sorting a specific value and then match the items:

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]));
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM