我正在尝试在c#中对文件进行排序,该文件包含字符串(列表中不同的星期几)。正常数组上的排序工作得到了一个例子
string[] array = { "Monday", "Thursday", "Monday", "Wednesday", "Friday", "Tuesday", "Friday", "Tuesday", "Thursday", "Wednesday" };
当通过排序算法时 - 按顺序输出日期:周一周一周五周五周四周四周二周二周三周三。但是当我将数组更改为文件数组(如下面的代码所示)时,它没有排序。我对c#编程很新。我试图使文件输出像上面那样。
我保留的代码中有一些部分,出于我自己的测试目的,所以我很抱歉这个混乱。会发生什么:
一些未显示的代码只是:用户输入的值介于1-10之间,这称为userFileInput。
我必须感谢任何帮助。谢谢!
CODE:
string[] value = MethodA();
//Console.WriteLine(value[1]);
string[] array = { value[userFileInput - 1] }; //userFileInput is the users choice of file
//string[] array = { "Monday", "Thursday", "Monday", "Wednesday", "Friday", "Tuesday", "Friday", "Tuesday", "Thursday", "Wednesday" };
InsertSort(array);
for (int x = 0; x < array.Length; x++)
Console.WriteLine(array[x]);
Console.WriteLine("Original File --------------------------------------------------\n");
Console.WriteLine(value[userFileInput - 1]);
Console.ReadKey();
}
static string[] MethodA() //FILE METHOD - READS IN THE FILES THEN RETURNS THEM TO MAIN METHOD
{
StreamReader dayFile = new StreamReader("c:..\\Files\\Day.txt"); StreamReader dateFile = new StreamReader("c:..\\Files\\Date.txt");
StreamReader sh1Open = new StreamReader("c:..\\Files\\SH1_Open.txt"); StreamReader sh1Close = new StreamReader("c:..\\Files\\SH1_Close.txt");
StreamReader sh1Volume = new StreamReader("c:..\\Files\\SH1_Volume.txt"); StreamReader sh1Diff = new StreamReader("c:..\\Files\\SH1_Diff.txt");
StreamReader sh2Open = new StreamReader("c:..\\Files\\SH2_Open.txt"); StreamReader sh2Close = new StreamReader("c:..\\Files\\SH2_Close.txt");
StreamReader sh2Volume = new StreamReader("c:..\\Files\\SH2_Volume.txt"); StreamReader sh2Diff = new StreamReader("c:..\\Files\\SH2_Diff.txt");
string dayString = dayFile.ReadToEnd(); string dateString = dateFile.ReadToEnd(); string Sh1OpenString = sh1Open.ReadToEnd(); string Sh1CloseString = sh1Close.ReadToEnd();
string Sh1VolumeString = sh1Volume.ReadToEnd(); string Sh1DiffString = sh1Diff.ReadToEnd(); string Sh2OpenString = sh2Open.ReadToEnd(); string Sh2CloseString = sh2Close.ReadToEnd();
string Sh2VolumeString = sh2Volume.ReadToEnd(); string Sh2DiffString = sh2Diff.ReadToEnd();
string[] fileArray = new string[] { Sh1OpenString, Sh1CloseString, Sh1VolumeString, Sh1DiffString , dateString , Sh2OpenString, Sh2CloseString, Sh2VolumeString, Sh2DiffString ,dayString };
return fileArray;
}
static void InsertSort(IComparable[] array)
{
int i, j;
for (i = 1; i < array.Length; i++)
{
IComparable value = array[i];
j = i - 1;
while ((j >= 0) && (array[j].CompareTo(value) > 0))
{
array[j + 1] = array[j];
j = j - 1;
}
array[j + 1] = value;
}
}
答案 0 :(得分:0)
我认为问题在于你在数组中保持这样的想法
string array = new[] { "Monday Saturday Monday", "Wednesday Thursday Monday" , ... };
看起来应该是这样的
string array = new[] { "Monday", "Saturday", "Monday", "Wednesday", "Thursday" , "Monday" , ... };
要制作这种数组,您可以遍历文件中的所有行并将其添加到列表中。
public List<string> ReadAllLines(StreamReader sr)
{
List<string> allLines = new List<string>();
string line;
while ((line = sr.ReadLine()) != null)
{
allLines.Add(line);
}
return allLines;
}
而不是这种类型的代码行
string dayString = dayFile.ReadToEnd();
您将此行与我们创建的方法一起使用,该方法将从文件中返回分隔的行。
List<string> dayString = ReadAllLines(dayFile);
// to the same with the rest of the files
从文件中读取所有行后,将其追加到fileArray
。完成数组后,您可以返回它。我们必须更改MethodA
中的string[][]
返回类型。
List<string[]> fileArray = new List<string>();
fileArray.Add(fileArray.ToArray());
// add from other files too
return fileArray.ToArray();
您可以像这样使用
// rest of the code
string[][] value = MethodA(); // notice string[][]
string[] array = value[userFileInput - 1]; //userFileInput is the users choice of file
InsertSort(array);
//rest of the code