高效地对 IList<T> 进行排序,而无需复制源列表。

7
给定以下测试用例,我该如何:
  1. 根据 IList<int> 列表中匹配的 Id 的索引对 IList<TestObject> 进行排序。
  2. 未匹配的值将移动到列表末尾,并按其原始索引排序。在这种情况下,由于 3 和 4 不存在于索引列表中,我们期望看到 list[3] == 3list[4] == 4
  3. 虽然我知道可以使用 Linq 实现此目标,但我需要重新对 原始 列表进行排序,而不是创建一个新列表(由于列表存储方式的限制)。
  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));
    }

更新:

根据要求,这是我尝试过的内容,但是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);
        });

更新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));
    }

@Mitch,我更新了我的问题。我已经让它工作了,但只能使用List<T> - Ben Foster
@Ben - 看一下我的回答,关于如何对你的IList进行原地排序,并可能使用更高效的比较方法...虽然我不认为它们会特别慢! - Jamiec
3个回答

2
您可以尝试以下方法:
ArrayList.Adapter(yourilist).Sort();

更新:

一个通用的比较器:

class Comparer<T> : IComparer<T>, IComparer
{
  internal Func<T, T, int> pred;

  public int Compare(T x, T y)
  {
    return pred(x, y);  
  }

  public int Compare(object x, object y)
  {
    return Compare((T)x, (T)y);
  }
}

static class ComparerExtensions
{
  static IComparer Create<T>(Func<T, T, int> pred)
  {
    return new Comparer<T> { pred = pred };
  }

  public static void Sort<T>(this ArrayList l, Func<T, T, int> pred)
  {
    l.Sort(Create(pred));
  }
}

用法:

ArrayList.Adapter(list).Sort<int>((x,y) => x - y);

ArrayList类提供了通用的Reverse、BinarySearch和Sort方法。这个包装器可以是在IList上使用这些方法的手段;然而,通过包装器执行这些通用操作可能比直接在IList上应用操作效率低下。 - Mitch Wheat
@Mitch Wheat:仅仅说“可能比直接应用于IList的操作效率低”是没有意义的,需要有解释。唯一不太高效的是多了一层间接性。 - leppie
@Ben:创建一个“通用”比较器类,以委托作为参数。 - leppie
1
@leppie:那不是我的话:这是ArrayList.Adapter的MSDN页面所述!建议您提交连接问题! - Mitch Wheat
@leppie,不错!我认为你已经赢了。 - Ben Foster
显示剩余2条评论

1

我已经研究了一段时间,正如之前所说,你需要使用ArrayList.Adapter,但是请注意它需要一个非泛型的IList,因此需要进行一些转换:

ArrayList.Adapter((IList)list)

您还需要编写一个比较器,其中包含进行排序的逻辑。请原谅这个名字:

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);
        }
    }
}

编辑:已将实现添加到上述比较器中

然后使用方法如下:

IList<int> indexList = new[] { 10, 5, 1, 9, 2 };
ArrayList.Adapter((IList)list).Sort(new WeirdComparer(indexList));

顺便说一下,这个主题解释了一种不错的方法来将其转化为扩展方法,这将使您的代码更具可重用性和易读性。

+1 - 被ArrayList.Adapter的非泛型要求所困扰,然后看到了你的答案 :) - Ben Foster
2
除了假设容器实现IList(不一定正确)之外,ArrayList.AdapterSort将列表中的项目复制到一个新数组中,对其进行排序,然后将它们复制回列表。不确定为什么会接受这种方法,因为问题标题要求避免复制源列表。在每种情况下,手动将其复制到普通数组中同样有效,可以保持所有内容的强类型,并因此避免了大量无意义的装箱。 - user743382

0
这是比较器的通用版本。IEntity<TId>只是一个简单的接口,具有类型为TId的属性“ Id”:
public class IndexComparer<T, TId> : Comparer<T> where T : IEntity<TId> where TId : IComparable
{
    private readonly IList<TId> order;
    private readonly int listCount;
    private readonly Func<T, int> currentIndexFunc;

    public IndexComparer(Func<T, int> currentIndexFunc, IList<TId> order, int listCount) {
        this.order = order;
        this.listCount = listCount;
        this.currentIndexFunc = currentIndexFunc;
    }

    public override int Compare(T x, T y)
    {
        var xIndex = order.IndexOf(x.Id);
        var yIndex = order.IndexOf(y.Id);

        if (xIndex == -1)
        {
            xIndex = listCount + currentIndexFunc(x);
        }
        if (yIndex == -1)
        {
            yIndex = listCount + currentIndexFunc(y);
        }

        return xIndex.CompareTo(yIndex);
    }
}

工作测试:

[TestFixture]
public class OrderingTests
{
    public class TestObject : IEntity<int>
    {
        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, 4 };
        ArrayList.Adapter((IList)list)
            .Sort(new IndexComparer<TestObject, int>(x => list.IndexOf(x), indexList, 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(4));
        Assert.That(list[4].Id, Is.EqualTo(3));
    }
}

这符合我原始问题中概述的要求。不匹配的元素被移动到列表末尾,然后按照它们的原始索引排序。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接