943,907 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Marked Solved
  • Views: 19327
  • C# RSS
Aug 11th, 2008
0

Sort/Filter Data in generic collection

Expand Post »
Hey guys,
I was trying to find some way to filter and sort data in a Generic Collection
of List<> Type but couldn't find any way to do this.
I have Tried to use BindingSource which didn't really filter any of the data in my collection.

Anyone have any idea how I can filter and sort data in a List<> Collection ?

Thanks!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
y_itay is offline Offline
5 posts
since Jul 2008
Aug 11th, 2008
0

Re: Sort/Filter Data in generic collection

There is no easy way to filter data with a generic collection unfortunately.

But sorting shouldn't be too hard since generic collections have a Sort function you can call. To implement a custom sorting algorithm check out this link on sorting points. It should help as a general guideline.

hope that helps
Reputation Points: 11
Solved Threads: 9
Junior Poster in Training
vckicks is offline Offline
58 posts
since Jun 2008
Aug 12th, 2008
0

Re: Sort/Filter Data in generic collection

Thanks
But what about filtering?
this is my main problem.
what's the best be to filter the List<> ?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
y_itay is offline Offline
5 posts
since Jul 2008
Aug 12th, 2008
0

Re: Sort/Filter Data in generic collection

It depends on what your doing with that list. Worst case, you can write a for loop that manually displays only the elements that aren't currently filtered out. Otherwise the only way I know off the top of my head is the Filter function in BindingSource, but that's more for DataSets...
Reputation Points: 11
Solved Threads: 9
Junior Poster in Training
vckicks is offline Offline
58 posts
since Jun 2008
Aug 12th, 2008
0

Re: Sort/Filter Data in generic collection

Personally, I'd make an interface... let's call it IFilterable with a required method let's call Filter.

C# Syntax (Toggle Plain Text)
  1. public interface {
  2. bool Filter( );
  3. }

The each object in the collection could have a member of type IFilterable that defines the filter is should be using and you could then foreach through the list calling each ones' .Filter method. This way you could have a nice plug'n'play filtering scheme that any list could call.

Next I'd create a class extension for the List<IFilterable> class.

C# Syntax (Toggle Plain Text)
  1. public static class ListEx {
  2. public static List<IFiltertable> Filter( this List<IFiltertable> list ) {
  3. List<IFiltertable> result = new List<IFiltertable>(list.Count);
  4.  
  5. foreach ( IFiltertable item in list ) {
  6. if ( item.Filter() ) {
  7. result.Add(item);
  8. }
  9. }
  10. }
  11. }

Now any List<IFilterable> I have I can just call Filter on it and get back a list of items that passed mustard.
Last edited by nvmobius; Aug 12th, 2008 at 3:54 am.
Reputation Points: 11
Solved Threads: 4
Light Poster
nvmobius is offline Offline
39 posts
since Jul 2008
Aug 12th, 2008
0

Re: Sort/Filter Data in generic collection

I found some nice example if anyone wanna look

http://www.paraesthesia.com/archive/...filtering.aspx
Reputation Points: 10
Solved Threads: 0
Newbie Poster
y_itay is offline Offline
5 posts
since Jul 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: Horizontal Image Scroller
Next Thread in C# Forum Timeline: How to append string in front





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC