我没有找到这个问题,所以我想问一下。我正在使用此代码
对2D数组进行排序for (long j = 0; j < Text.Length; j++)
{
if (AbsMinDis[i] < AbsMinDis[PosMin])
{
CurXpos = XPos[i];
CurYpos = YPos[i];
string swap = Word[PosMin, j];
Word[PosMin, j] = Word[i, j];
Word[i, j] = swap;
PosMin = i;
}
}
然后,我这样写:
for (long i = 0; i < Text.Length; i++)
{
FileSorted.Write(Word[i, 0]);
for (byte k = 1; k < 8; k++)
{
FileSorted.Write(" {0}", Word[i, k]);
}
FileSorted.WriteLine();
}
FileSorted.Close();
它的作用是以原始的顺序重写代码。我使用此代码查找元素AbsMinDis
之间的最近距离,然后对它们进行排序并写入文件。
谢谢你的帮助
变量
StreamWriter FileSorted = new StreamWriter("C://Users//Tomas//Desktop//Bakalárka//Fiel test//Fiel test//tmp//Data.txt");
long PosMin = 0; //position of line with minimum
for (long i = 0; i < Text.Length; i++) // search for minimal distance
{
double X1 = Math.Round(double.Parse(Word[i, 1], System.Globalization.CultureInfo.InvariantCulture), 3); // X start possition to double
double Y1 = Math.Round(double.Parse(Word[i, 2], System.Globalization.CultureInfo.InvariantCulture), 3); //Y , etc.
double X2 = Math.Round(double.Parse(Word[i, 3], System.Globalization.CultureInfo.InvariantCulture), 3);
double Y2 = Math.Round(double.Parse(Word[i, 4], System.Globalization.CultureInfo.InvariantCulture), 3);
double[] XPos = new double[Text.Length]; // array of smallest distances for each line
double[] YPos = new double[Text.Length];
double MinDis1 = Math.Sqrt(Math.Pow(X1 - CurXpos, 2) + Math.Pow(Y1 - CurXpos, 2)); //calculation of the smallest distances
double MinDis2 = Math.Sqrt(Math.Pow(X2 - CurXpos, 2) + Math.Pow(Y2 - CurYpos, 2)); //calculate if end points are closer
double[] AbsMinDis = new double[Text.Length]; // line containing distance data of each line
if (MinDis1 < MinDis2) // if distance of starting coordinate is smaller than ending, save