谁能帮忙,我做排序有问题,我以为我已经排序但似乎没有工作。
我有一个存储以下值的列表
8,6,10,11,7
我还有另一个列表(我的类中的附件,它有一个名为 accessoryId 的属性,当前类的 id 顺序为 6、7、8、10、11)
因此,我需要将它们从 6、7、8、10、11 排序到简单列表中使用的顺序,即 8、6、10、11、7
我有我的 icomparable (见下文),我这样打电话 - 它确实输入但有问题,因为列表仍然包含我的所有课程,但仍按 6、7、8、10、11 的顺序
// accesories is the IList<Accessories> (hence why i am use ToList)
// and sortOrder is the simple int list list<int>
accesories.ToList().Sort(new ItemTpComparer(sortOrder));
class ItemTpComparer : IComparer<Accessories>
{
private IList<int> otherList;
public ItemTpComparer(IList<int> otherList)
{
this.otherList = otherList;
}
#region IComparer<Accessories> Members
public int Compare(Accessories x, Accessories y)
{
if (otherList.IndexOf(x.AccessoryId) > otherList.IndexOf(y.AccessoryId))
return 1;
else if (otherList.IndexOf(x.AccessoryId) < otherList.IndexOf(y.AccessoryId))
return -1;
else
return 0;
// tried below also didn't work
//return otherList.IndexOf(x.AccessoryId) - otherList.IndexOf(y.AccessoryId);