Sort/Filter Data in generic collection

Please support our C# advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Jul 2008
Posts: 5
Reputation: y_itay is an unknown quantity at this point 
Solved Threads: 0
y_itay y_itay is offline Offline
Newbie Poster

Sort/Filter Data in generic collection

 
0
  #1
Aug 11th, 2008
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!
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 58
Reputation: vckicks is an unknown quantity at this point 
Solved Threads: 9
vckicks vckicks is offline Offline
Junior Poster in Training

Re: Sort/Filter Data in generic collection

 
0
  #2
Aug 11th, 2008
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
Visual C# Kicks - Free C# code resources and articles.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 5
Reputation: y_itay is an unknown quantity at this point 
Solved Threads: 0
y_itay y_itay is offline Offline
Newbie Poster

Re: Sort/Filter Data in generic collection

 
0
  #3
Aug 12th, 2008
Thanks
But what about filtering?
this is my main problem.
what's the best be to filter the List<> ?
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 58
Reputation: vckicks is an unknown quantity at this point 
Solved Threads: 9
vckicks vckicks is offline Offline
Junior Poster in Training

Re: Sort/Filter Data in generic collection

 
0
  #4
Aug 12th, 2008
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...
Visual C# Kicks - Free C# code resources and articles.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 39
Reputation: nvmobius is an unknown quantity at this point 
Solved Threads: 4
nvmobius nvmobius is offline Offline
Light Poster

Re: Sort/Filter Data in generic collection

 
0
  #5
Aug 12th, 2008
Personally, I'd make an interface... let's call it IFilterable with a required method let's call Filter.

  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.

  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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 5
Reputation: y_itay is an unknown quantity at this point 
Solved Threads: 0
y_itay y_itay is offline Offline
Newbie Poster

Re: Sort/Filter Data in generic collection

 
0
  #6
Aug 12th, 2008
I found some nice example if anyone wanna look

http://www.paraesthesia.com/archive/...filtering.aspx
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the C# Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC