C#按元素之间的距离对文件进行排序

C#按元素之间的距离对文件进行排序,c#,arrays,file,sorting,distance,C#,Arrays,File,Sorting,Distance,我有一个类似这样的文本文件 LINE 325,474 195,251 589,821 375,711 Nan Nan Nan LINE 617,303 578,402 771,724 392,711 Nan Nan Nan LINE 424,931 472,48 481,203 617,633 Nan Nan Nan 其中第一列基本上是元素的名称,第二列是起始X坐标,第三列是起始Y坐标,第四列和第五列是结束X和Y坐标,其他的不重要。 我需要按每行之间的距离对它们进行排序。我的代码如下所示:

我有一个类似这样的文本文件

LINE 325,474 195,251 589,821 375,711 Nan Nan Nan
LINE 617,303 578,402 771,724 392,711 Nan Nan Nan
LINE 424,931 472,48 481,203 617,633 Nan Nan Nan
其中第一列基本上是元素的名称,第二列是起始X坐标,第三列是起始Y坐标,第四列和第五列是结束X和Y坐标,其他的不重要。 我需要按每行之间的距离对它们进行排序。我的代码如下所示:

        string[] Text = File.ReadAllLines(OpenFile.Filename);
        string[,] Word = new string[Text.Length, 8];
        double CurXpos = 0; // for smallest distance set new points (starting from 0,0)
        double CurYpos = 0;
        string[] word = new string[8];
        for (long i = 0; i < Text.Length; i++) // read text 
        {                
            string line = Text[i];               

                for (byte j = 0; j < 8; j++)
                {
                    word = line.Split(' ');
                    Word[i, j] = word[j]; //store text to 2D array
                }
        }
        StreamWriter FileSorted = new StreamWriter(desired destination);
        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                
            long PosMin = 0; //position of line with minimum
            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 
            {
                AbsMinDis[i] = MinDis1;
                XPos[i] = X1;
                YPos[i] = Y1;

            }
            else if (MinDis2 < MinDis1) // if distance of ending points is smaller, swap starting end endinng points and save line possition
            {
                AbsMinDis[i] = MinDis2;
                XPos[i] = X2;
                YPos[i] = Y2;
                Word[i, 1] = X2.ToString();
                Word[i, 2] = Y2.ToString();
                Word[i, 3] = X1.ToString();
                Word[i, 4] = Y1.ToString();                    
            }
            for (long j = 0; j < Text.Length; j++) //sorting file 
            {
                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;
                }             
            FileSorted.Write(Word[i, 0]);
            for (byte k = 1; k < 8; k++)
            {
                FileSorted.Write(" {0}", Word[i, k]);
            }
            FileSorted.WriteLine();
        }
        FileSorted.Close();
string[]Text=File.ReadAllLines(OpenFile.Filename);
字符串[,]Word=新字符串[Text.Length,8];
双CurXpos=0;//对于最小距离,设置新点(从0,0开始)
双CurYpos=0;
字符串[]字=新字符串[8];
for(long i=0;i
现在我不知道我是否有任何错误,或者我是否不知道如何编写它,因为它看起来好像对文件没有任何作用 写作是这样的:

        string[] Text = File.ReadAllLines(OpenFile.Filename);
        string[,] Word = new string[Text.Length, 8];
        double CurXpos = 0; // for smallest distance set new points (starting from 0,0)
        double CurYpos = 0;
        string[] word = new string[8];
        for (long i = 0; i < Text.Length; i++) // read text 
        {                
            string line = Text[i];               

                for (byte j = 0; j < 8; j++)
                {
                    word = line.Split(' ');
                    Word[i, j] = word[j]; //store text to 2D array
                }
        }
        StreamWriter FileSorted = new StreamWriter(desired destination);
        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                
            long PosMin = 0; //position of line with minimum
            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 
            {
                AbsMinDis[i] = MinDis1;
                XPos[i] = X1;
                YPos[i] = Y1;

            }
            else if (MinDis2 < MinDis1) // if distance of ending points is smaller, swap starting end endinng points and save line possition
            {
                AbsMinDis[i] = MinDis2;
                XPos[i] = X2;
                YPos[i] = Y2;
                Word[i, 1] = X2.ToString();
                Word[i, 2] = Y2.ToString();
                Word[i, 3] = X1.ToString();
                Word[i, 4] = Y1.ToString();                    
            }
            for (long j = 0; j < Text.Length; j++) //sorting file 
            {
                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;
                }             
            FileSorted.Write(Word[i, 0]);
            for (byte k = 1; k < 8; k++)
            {
                FileSorted.Write(" {0}", Word[i, k]);
            }
            FileSorted.WriteLine();
        }
        FileSorted.Close();
FileSorted.Write(Word[i,0]);
用于(字节k=1;k<8;k++)
{
Write(“{0}”,字[i,k]);
}
FileSorted.WriteLine();
}
FileSorted.Close();

感谢您的时间和帮助。

i
的其他迭代(第一次除外)中,写入
SortedFile
失败,因为您在迭代结束时关闭了文件:

FileSorted.Close();
如果在完成了
i
的所有迭代之后移动
FileSorted.Close()
,则会看到
SortedFile
中的所有行

行重复是因为当写入到您注释/排序文件的文件时,您将行
i
写入多次而不是一次


您可以看到大写惯例。

i
的其他迭代(第一次除外)中写入
SortedFile
失败,因为您在迭代结束时关闭了文件:

FileSorted.Close();
如果在完成了
i
的所有迭代之后移动
FileSorted.Close()
,则会看到
SortedFile
中的所有行

行重复是因为当写入到您注释/排序文件的文件时,您将行
i
写入多次而不是一次


你可以看到大写惯例。

如果你在开头用一个小字母写变量名,阅读起来会更容易。这是什么意思?对不起,我是C#新手,那么代码执行的结果是什么?有错误吗?空文件吗?根本没有文件吗?@DanielFrühauf-这都是个人喜好和背景的问题。你知道吗esn对OP的问题一点帮助都没有。重写文件,或糟糕的排序@kernelmode如果你在开头用一个小字母写变量名,阅读起来会更容易。你是什么意思?对不起,我是C#新手,那么代码执行的结果是什么?有错误吗?空文件?根本没有文件?@DanielFrühauf-都是问题个人喜好和背景。对OP的问题没有任何帮助。重写文件,或者@KernelMode排序错误