| | |
Sort/Filter Data in generic collection
Please support our C# advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Jul 2008
Posts: 5
Reputation:
Solved Threads: 0
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!
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!
•
•
Join Date: Jun 2008
Posts: 58
Reputation:
Solved Threads: 9
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
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.
•
•
Join Date: Jun 2008
Posts: 58
Reputation:
Solved Threads: 9
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.
•
•
Join Date: Jul 2008
Posts: 39
Reputation:
Solved Threads: 4
Personally, I'd make an interface... let's call it IFilterable with a required method let's call Filter.
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.
Now any List<IFilterable> I have I can just call Filter on it and get back a list of items that passed mustard.
C# Syntax (Toggle Plain Text)
public interface { bool Filter( ); }
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)
public static class ListEx { public static List<IFiltertable> Filter( this List<IFiltertable> list ) { List<IFiltertable> result = new List<IFiltertable>(list.Count); foreach ( IFiltertable item in list ) { if ( item.Filter() ) { result.Add(item); } } } }
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.
•
•
Join Date: Jul 2008
Posts: 5
Reputation:
Solved Threads: 0
I found some nice example if anyone wanna look
http://www.paraesthesia.com/archive/...filtering.aspx
http://www.paraesthesia.com/archive/...filtering.aspx
![]() |
Other Threads in the C# Forum
- Previous Thread: Horizontal Image Scroller
- Next Thread: How to append string in front
| Thread Tools | Search this Thread |
.net access ado.net algorithm array barchart bitmap box broadcast buttons c# check checkbox client color combo combobox control conversion csharp custom database datagrid datagridview dataset datetime degrees developer development draganddrop drawing encryption enum equation event excel file files form format forms function gdi+ httpwebrequest image index input install java label list listbox listener mandelbrot math mathematics mouseclick mysql nargalax operator path photoshop picturebox pixelinversion post programming radians regex remote remoting restore richtextbox save saving serialization server sleep socket sql stack statistics stream string table tcp text textbox thread time timer update usercontrol validation view visualstudio webbrowser windows winforms wpf xml





