Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/263.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/5/spring-mvc/2.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# 从文件中读取数据,并使用c对数据进行排序_C# - Fatal编程技术网

C# 从文件中读取数据,并使用c对数据进行排序

C# 从文件中读取数据,并使用c对数据进行排序,c#,C#,我需要从文本文件中读取2D数组,并按升序对列进行排序 我目前的做法是: 1从文件读取数据并根据空格分割值 2并将3列值指定给3个ArrayList 我尝试用Sort方法对每个Arraylist进行排序。但无法获得所需的输出,它只对正值进行排序&而不是负值 代码示例 ArrayList col1 = new ArrayList(); ArrayList col2 = new ArrayList(); ArrayList col3 = new Arra

我需要从文本文件中读取2D数组,并按升序对列进行排序

我目前的做法是:

1从文件读取数据并根据空格分割值 2并将3列值指定给3个ArrayList

我尝试用Sort方法对每个Arraylist进行排序。但无法获得所需的输出,它只对正值进行排序&而不是负值

代码示例

        ArrayList col1 = new ArrayList();
        ArrayList col2 = new ArrayList();
        ArrayList col3 = new ArrayList();
        using (StreamReader sr = new StreamReader(@"E:\ArrayCol1.txt"))
        {
            string line;
            while ((line = sr.ReadLine()) != null)
            {
                string[] split = Regex.Split(line, "[ \t]");

                if (split.Count() == 3)
                {
                    col1.Add(Convert.ToInt32(split[0]));
                    col2.Add(Convert.ToInt32(split[1]));
                    col3.Add(Convert.ToInt32(split[2]));
                }
            }
        }
        col1.Sort();
        foreach (int s in col1)
            Console.WriteLine(s);
文本文件数据数组[4,3]:-

50 10 77

-15-215-1

-20-600-200

10-70 1000

预期产量

50 10 1000

10-70 77

-15-215-1

-20-600-200

你试过调试吗

排序按升序排序。我必须使用OrderByDescending使其下降

重新编写代码:

List<int> col1 = new List<int>();
    List<int> col2 = new List<int>();
    List<int> col3 = new List<int>();

    using (var reader = new StreamReader(@"C:\users\susingh\desktop\data.txt"))
    {
        string line;
        while (true)
        {
            line = reader.ReadLine();
            if (line==null)
            {
                break;
            }
            var numArray = line.Split('\t');
            col1.Add(Int32.Parse(numArray[0]));
            col2.Add(Int32.Parse(numArray[1]));
            col3.Add(Int32.Parse(numArray[2]));
        }
    }

    var Foo = col1.OrderByDescending(x=>x);
    foreach (var element in Foo)
    {
        Console.WriteLine (element);
    }

请告诉我们您实现了什么代码@你为什么要使用ArrayList?
ArrayList col1 = new ArrayList();
ArrayList col2 = new ArrayList();
ArrayList col3 = new ArrayList();
using (StreamReader sr = new StreamReader(@"Sample.txt"))
{
    string line;
    while ((line = sr.ReadLine()) != null)
    {
        string[] split = Regex.Split(line, @"\s+");

        if (split.Count() == 3)
        {
            col1.Add(Convert.ToInt32(split[0]));
            col2.Add(Convert.ToInt32(split[1]));
            col3.Add(Convert.ToInt32(split[2]));
        }
    }
}

// sort all columns
col1.Sort();
col2.Sort();
col3.Sort();

// output is largest to smallest so invert sort
for (int i = col1.Count -1 ; i >= 0; i--)
{
    Console.WriteLine(string.Format("{0}\t{1}\t{2}", col1[i], col2[i], col3[i]));
}