Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6152351
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T19:54:21+00:00 2026-05-23T19:54:21+00:00

Given the test case below how can I: Sort the IList<TestObject> based on the

  • 0

Given the test case below how can I:

  1. Sort the IList<TestObject> based on the index of a matching Id
    in the IList<int> list.
  2. Unmatched values are moved to the end of the list and sorted by their original index. In this case, since 3 and 4 do not exist in the index list, we expect to see list[3] == 3 and list[4] == 4.
  3. Whilst I know this can be achieved with linq, I need to resort the original list rather than creating a new one (due to how the list is stored).
  4. The source list must be an IList (I can’t use List<T>)

Here’s the test:

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

Update:

As requested, this is what I did try, but 1) it only works with List<T> and 2) I’m not sure it’s the most efficient way:

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

Update 2:

Thanks to @leppie, @jamiec, @mitch wheat – this is the working code:

    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));
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T19:54:22+00:00Added an answer on May 23, 2026 at 7:54 pm

    Been looking at this for a bit, and indeed as previously said, your going to need ArrayList.Adapter, however you’ll note it takes a non-generic IList so some casting will be required:

    ArrayList.Adapter((IList)list)
    

    You’ll also need to write a comparer, of which the logic to do your sorting willl be contained. Excuse the name but:

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

    EDIT: Added implementation to above comparerr

    Then the usage would be as follows:

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

    By the way, this thread explains a nice way to turn this into an extension method which will make your code more reusable and easier to read IMO.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The test case given below shows a simple case where I have 2 parameters
Given the URL (single line): http://test.example.com/dir/subdir/file.html How can I extract the following parts using
How can I test if a mouse is within a given div? I know
In most languages like C# for example given a string you can test (boolean)
The test class below verifies that a simple HttpService gets content from a given
Given test.txt containing: test message I want to end up with: testing a message
Given an object like: class M(models.Model): test = models.BooleanField() created_date = models.DateTimeField(auto_now_add=True) Given sample
given a string say a 19 b c d 20, how do I test
I need to test whether various types of database objects exist in a given
I'd like to make sure a given view in my test is fetching an

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.