我需要从文本文件中读取2D数组,然后按升序对列进行排序。
我目前的实施是:
1)从文件中读取数据&根据空格分割值 2)并为3个Arraylists分配3个列值
我尝试使用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
答案 0 :(得分:0)
您是否尝试过调试?
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);
}
让它下降。
重新编写代码:
col=pygame.sprite.spritecollide(carImg,hit_list,False)
答案 1 :(得分:0)
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]));
}