在不复制源列表的情况下有效排序IList <T>

鉴于下面的测试案例,我该如何:

  • 根据IList<int>列表中匹配Id的索引对IList<TestObject>排序。
  • 不匹配的值将移动到列表的末尾并按其原始索引排序。 在这种情况下,由于3和4不存在于索引列表中,我们期望看到list[3] == 3list[4] == 4
  • 虽然我知道这可以通过linq来实现,但我需要使用原始列表而不是创建一个新列表(由于列表存储的方式)。
  • 源列表必须是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小麦 - 这是工作代码:

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

    一直在寻找这一点,事实上如前所述,你将需要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));
    

    顺便说一句,这个线程解释了一个很好的方法来将它变成一个扩展方法,这将使你的代码更加可重用并且更易于读取IMO。


    您可以尝试以下方法:

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

    这是比较器的通用版本。 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));
        }
    }
    

    这符合我原始问题中列出的要求。 不匹配的元素将移动到列表的末尾,然后按其原始索引排序。

    链接地址: http://www.djcxy.com/p/53609.html

    上一篇: Efficiently sort an IList<T> without copying the source list

    下一篇: How to show loading animation in Android imageview?