这是我在文件中的输入商店:
50|Carbon|Mercury|M:4;C:40;A:1
90|Oxygen|Mars|M:10;C:20;A:00
90|Serium|Jupiter|M:3;C:16;A:45
85|Hydrogen|Saturn|M:33;C:00;A:3
此处 50,90,90,85 表示权重, M,C,A 表示此元素中的每个元素的比例。
现在我想显示每个元素(即碳,氧等)从最高到最低,如果多个元素具有相同的权重,请将它们分组到单个重量并按行星(火星,木星)按字母顺序排序 元素(碳,氧气等)
预期输出:
1)90
Serium;Jupiter (sorted alphabetically by planet name).
compounds:M:3;C:16;A:45
Oxygen;Mars
compounds:M:10;C:20;A:00
2)85
Hydrogen;Saturn
M:33;C:00;A:3
3)50
Carbon;Mercury
M:4;C:40;A:1
这就是我的表现:
public class Planets
{
public int Number { get; set; } //This field points to first cell of every row.output 50,90,90,85
public string name { get; set; } //This field points to Second cell of every row.output Hallogen,Oxygen,Hydrogen
public string object { get; set; } ////This field points to third cell of every row.output Mercury,Mars,Saturn
public List<proportion> proportion { get; set; } //This will store all proportions with respect to planet object.
//for Hallogen it will store 4,40,1.Just store number.ignore M,C,A initials.
//for oxygen it will store 10,20,00.Just store number.ignore M,C,A initials.
}
public class proportion
{
public int Number { get; set; }
}
List<Planets> Planets = new List<Planets>();
using (StreamReader sr = new StreamReader(args[0]))
{
String line;
while ((line = sr.ReadLine()) != null)
{
Planets planet = new Planets();
String[] parts = line.Split('|');
planet.Number = Convert.ToInt32(parts[0]);
planet.name = parts[1];
planet.obj = parts[2];
String[] smallerParts = parts[3].Split(';');
planet.proportion = new List<proportion>();
foreach (var item in smallerParts)
{
proportion prop = new proportion();
prop.Number =
Convert.ToInt32(item.Split(':')[1]);
planet.proportion.Add(prop);
}
Planets.Add(planet);
}
}
var data = Planets.OrderByDescending(t => t.Number).ToList();//Highest to lowest.
foreach (var item in data)
{
//What to do for same elements
}
我成功地能够在我的星球列表对象中添加所有4行,如下所示:
Planets[0]:
{
Number:50
name: Carbon
object:Mercury
proportion[0]:
{
Number:4
},
proportion[1]:
{
Number:40
},
proportion[2]:
{
Number:1
}
}
Etc.......
这里唯一的问题是显示相同数量的重量(预期输出1 )并按行星(火星,木星)按字母顺序排序,然后按元素排序(碳,氧气等。)
答案 0 :(得分:1)
我在LinqPad上创建了以下代码以实现您期望的结果,如果您仍需要更改,请告诉我,简单的Linq查询将有助于实现结果:
void Main()
{
List<Input> customList = Input.Create();
var result = customList.GroupBy(x=>x.Weight,x=>new {x.Element1,x.Element2,x.Detail})
.Select(y=>new {
key = y.Key,
collection = y.OrderBy(z=>z.Element2)
}
).OrderByDescending(h=>h.key);
foreach(var n in result)
{
Console.WriteLine("Weight :: " + n.key);
foreach(var g in n.collection)
{
Console.WriteLine(g.Element1 + ";" + g.Element2);
Console.WriteLine("Compounds:" + g.Detail);
}
}
}
public class Input
{
public int Weight {get; set;}
public string Element1 {get; set;}
public string Element2 {get; set;}
public string Detail {get; set;}
public Input(int w, string e1, string e2, string d)
{
Weight = w;
Element1 = e1;
Element2 = e2;
Detail = d;
}
public static List<Input> Create()
{
List<Input> returnList = new List<Input>();
returnList.Add(new Input(50,"Carbon","Mercury","M:4;C:40;A:1"));
returnList.Add(new Input(90,"Oxygen","Mars","M:10;C:20;A:00"));
returnList.Add(new Input(90,"Serium","Jupiter","M:3;C:16;A:45"));
returnList.Add(new Input(85,"Hydrogen","Saturn","M:33;C:00;A:3"));
return (returnList);
}
}