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

  • Home
  • SEARCH
  • 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 7532595
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T05:30:00+00:00 2026-05-30T05:30:00+00:00

I have a generic collection: public Items : Collection<Object> { protected override void InsertItem(int

  • 0

I have a generic collection:

public Items : Collection<Object>
{
   protected override void InsertItem(int index, Object item)
   {
      base.InsertItem(index, item);
      ...
   }

   protected override void RemoveItem(int index)
   {
      base.RemoveItem(index);
      ...
   }

   protected override void SetItem(int index, Object item)
   {
      base.SetItem(index, item);
      ...
   }

   protected override void ClearItems()
   {
      base.ClearItems();
      ...
   }

Now I need a way to sort this collection in-place.

Bonus Chatter

I tried converting my class to use List<T> rather than Collection<T> (since Collection<T> doesn’t support the concept of an order). That then allowed calling the Sort method:

this.Items.Sort(SortCompareCallback);

protected virtual int SortCompareCallback(Object x, Object y)
{
   return OnCompareItems(new SortCompareEventArgs(x, y, this.sortColumnIndex, direction));
}

But then I lose the virtual methods when the list is modified.

I thought about using Linq, but the problem with that is:

  • I don’t know how to call a callback from a Linq expression
  • Linq doesn’t sort a collection, it can only return a new one

How can I sort a generic Collection<T>?

  • 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-30T05:30:02+00:00Added an answer on May 30, 2026 at 5:30 am

    If you don’t need to have the virtual overrides called during the sorting, you should be able to do something like this:

    class SortableCollection<T> : Collection<T>
    {
        private readonly List<T> _list;
    
        public SortableCollection() : this(new List<T>()) {}
        public SortableCollection(List<T> list) : base(list)
        {
            _list = list;
        }
        public void Sort() { _list.Sort(); }
    }
    

    Or this:

    class SortableCollection<T> : Collection<T>
    {
        public SortableCollection() : this(new List<T>()) {}
        public SortableCollection(List<T> list) : base(list) {}
        public void Sort() { ((List<T>)Items).Sort(); }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Why isn't Collection.remove(Object o) generic? Seems like Collection<E> could have boolean remove(E o); Then,
I have the following code : using System.Collections.Generic; public class Test { static void
I have this code (C#): using System.Collections.Generic; namespace ConsoleApplication1 { public struct Thing {
I have a generic collection of type MyImageClass, and MyImageClass has an boolean property
I have a generic Collection and am trying to work out how I can
I have a system.collections.generic.list(of ListBox) I would like to use the collection classes built-in
Suppose I have a collection (be it an array, generic List, or whatever is
I have constructed a generic method which deserialiazes an object in the following manner:
I have the following code, where i am trying to create a generic collection
I have a generic class containing a special collection. An instance of this collection

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.