Ben*_*ter 7 .net c# collections
鉴于下面的测试用例我怎么能:
IList<TestObject>基于匹配的索引Id
中的IList<int>列表.list[3] == 3和list[4] == 4.IList(我不能使用List<T>)这是测试:
public class TestObject
{
public int Id { get; set; }
}
[Test]
public void Can_reorder_using_index_list()
{
IList<TestObject> list = new List<TestObject>
{
new TestObject { Id = 1 },
new TestObject { Id = 2 },
new TestObject { Id = 3 },
new TestObject { Id = 4 },
new TestObject { Id = 5 }
};
IList<int> indexList = new[] { 10, 5, 1, 9, 2 };
// TODO sort
Assert.That(list[0].Id, Is.EqualTo(5));
Assert.That(list[1].Id, Is.EqualTo(1));
Assert.That(list[2].Id, Is.EqualTo(2));
Assert.That(list[3].Id, Is.EqualTo(3));
Assert.That(list[4].Id, Is.EqualTo(4));
}
Run Code Online (Sandbox Code Playgroud)
更新:
根据要求,这是我尝试过的,但1)它只适用于List<T>和2)我不确定它是最有效的方式:
var clone = list.ToList();
list.Sort((x, y) =>
{
var xIndex = indexList.IndexOf(x.Id);
var yIndex = indexList.IndexOf(y.Id);
if (xIndex == -1)
{
xIndex = list.Count + clone.IndexOf(x);
}
if (yIndex == -1)
{
yIndex = list.Count + clone.IndexOf(y);
}
return xIndex.CompareTo(yIndex);
});
Run Code Online (Sandbox Code Playgroud)
更新2:
感谢@leppie,@ jamiec,@ mitch wheat - 这是工作代码:
public class TestObjectComparer : Comparer<TestObject>
{
private readonly IList<int> indexList;
private readonly Func<TestObject, int> currentIndexFunc;
private readonly int listCount;
public TestObjectComparer(IList<int> indexList, Func<TestObject, int> currentIndexFunc, int listCount)
{
this.indexList = indexList;
this.currentIndexFunc = currentIndexFunc;
this.listCount = listCount;
}
public override int Compare(TestObject x, TestObject y)
{
var xIndex = indexList.IndexOf(x.Id);
var yIndex = indexList.IndexOf(y.Id);
if (xIndex == -1)
{
xIndex = listCount + currentIndexFunc(x);
}
if (yIndex == -1)
{
yIndex = listCount + currentIndexFunc(y);
}
return xIndex.CompareTo(yIndex);
}
}
[Test]
public void Can_reorder_using_index_list()
{
IList<TestObject> list = new List<TestObject>
{
new TestObject { Id = 1 },
new TestObject { Id = 2 },
new TestObject { Id = 3 },
new TestObject { Id = 4 },
new TestObject { Id = 5 }
};
IList<int> indexList = new[] { 10, 5, 1, 9, 2, 4 };
ArrayList.Adapter((IList)list).Sort(new TestObjectComparer(indexList, x => list.IndexOf(x), list.Count));
Assert.That(list[0].Id, Is.EqualTo(5));
Assert.That(list[1].Id, Is.EqualTo(1));
Assert.That(list[2].Id, Is.EqualTo(2));
Assert.That(list[3].Id, Is.EqualTo(3));
Assert.That(list[4].Id, Is.EqualTo(4));
}
Run Code Online (Sandbox Code Playgroud)
已经研究了一会儿,确实如前所述,您将需要ArrayList.Adapter,但是您会注意到它需要一个非通用 IList,因此需要进行一些转换:
ArrayList.Adapter((IList)list)
Run Code Online (Sandbox Code Playgroud)
您还需要编写一个比较器,其中将包含进行排序的逻辑。请原谅这个名字,但是:
public class WeirdComparer : IComparer,IComparer<TestObject>
{
private IList<int> order;
public WeirdComparer(IList<int> order)
{
this.order = order;
}
public int Compare(object x, object y)
{
return Compare((TestObject) x, (TestObject) y);
}
public int Compare(TestObject x, TestObject y)
{
if(order.Contains(x.Id))
{
if(order.Contains(y.Id))
{
return order.IndexOf(x.Id).CompareTo(order.IndexOf(y.Id));
}
return -1;
}
else
{
if (order.Contains(y.Id))
{
return 1;
}
return x.Id.CompareTo(y.Id);
}
}
}
Run Code Online (Sandbox Code Playgroud)
编辑:添加了上面的comparerr的实现
那么用法如下:
IList<int> indexList = new[] { 10, 5, 1, 9, 2 };
ArrayList.Adapter((IList)list).Sort(new WeirdComparer(indexList));
Run Code Online (Sandbox Code Playgroud)
顺便说一句,该线程解释了一种将其转换为扩展方法的好方法,这将使您的代码更具可重用性并且更易于阅读(IMO)。
| 归档时间: |
|
| 查看次数: |
2736 次 |
| 最近记录: |