我有一组对象,它们本身包含一组对象。
private class Pilot
{
public string Name;
public HashSet<string> Skills;
}
这里是一些测试数据:
public void TestSetComparison()
{
var pilots = new[]
{
new Pilot { Name = "Smith", Skills = new HashSet<string>(new[] { "B-52", "F-14" }) },
new Pilot { Name = "Higgins", Skills = new HashSet<string>(new[] { "Concorde", "F-14" }) },
new Pilot { Name = "Jones", Skills = new HashSet<string>(new[] { "F-14", "B-52" }) },
new Pilot { Name = "Wilson", Skills = new HashSet<string>(new[] { "F-14", "Concorde" }) },
new Pilot { Name = "Celko", Skills = new HashSet<string>(new[] { "Piper Cub" }) },
};
我想OrderBy
在Linq中使用:
我想我需要实现一个IComparer<Pilot>
传递给,OrderBy
但不知道如何处理“无所谓”方面(上面)和稳定排序。
更新:
我希望输出是相同五个对象的数组,但顺序不同。 Pilot
当GroupBy
您必须IEqualityComparer<T>
为类型进行实现时(HashSet<string>
例如)
private sealed class MyComparer : IEqualityComparer<HashSet<string>> {
public bool Equals(HashSet<string> x, HashSet<string> y) {
if (object.ReferenceEquals(x, y))
return true;
else if (null == x || null == y)
return false;
return x.SetEquals(y);
}
public int GetHashCode(HashSet<string> obj) {
return obj == null ? -1 : obj.Count;
}
}
然后使用它:
IEnumerable<Pilot> result = pilots
.GroupBy(pilot => pilot.Skills, new MyComparer())
.Select(chunk => string.Join(", ", chunk
.Select(item => item.Name)
.OrderBy(name => name))); // drop OrderBy if you want stable Smith, Jones
Console.WriteLine(string.Join(Environment.NewLine, result));
结果:
Jones, Smith
Higgins, Wilson
Celko
编辑:如果要重新排列一个数组,则添加SelectMany()
以使分组扁平化,最后一个ToArray()
:
var result = pilots
.GroupBy(pilot => pilot.Skills, new MyComparer())
.SelectMany(chunk => chunk)
.ToArray();
Console.WriteLine(string.Join(", ", result.Select(p => p.Name)));
结果:
Jones, Smith,
Higgins, Wilson,
Celko
请注意,string.join在一行中组合了每个组的名称,即,Jones, Smith
两者具有相同的技能。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句