Re: SortedList of Files using IComparer
- From: "Ignacio Machin \( .NET/ C# MVP \)" <ignacio.machin AT dot.state.fl.us>
- Date: Fri, 24 Jun 2005 10:12:41 -0400
hi,
you have a collection of what ? File has all its methods static so you
cannot use it.
FileInfo maybe? I will assume it.
I did a while ago a class to compare any kind of class, it was my first real
reflection code IIRC :)
It;'s very simple of use all you have to do is pass to the constructor the
name of the property and the ordering you want ( descending or ascending )
like:
new ClassSorter( "ListWriteTime", SortDirection.Ascending );
you could also use a property of the property:
new ClassSorter( "ListWriteTime.Hour", SortDirection.Ascending );
Hope this help, let me know if you need help with the code
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
*******************************************************
public class ClassSorter: IComparer
{
protected string sortBy;
protected SortByType sortByType;
protected SortDirection sortDirection;
#region Constructors
public ClassSorter(string sortBy, SortByType sortByType, SortDirection
sortDirection)
{
this.sortBy = sortBy;
this.sortByType = sortByType;
this.sortDirection = sortDirection;
}
#endregion
int Compare( object x, object y, string comparer)
{
if ( comparer.IndexOf( ".") != -1 )
{
//split the string
string[] parts = comparer.Split( new char[]{ '.'} );
return Compare( x.GetType().GetProperty( parts[0]).GetValue(x, null) ,
y.GetType().GetProperty( parts[0]).GetValue(y, null) , parts[1]
);
}
else
{
IComparable icx, icy;
icx =
(IComparable)x.GetType().GetProperty( comparer).GetValue(x, null);
icy =
(IComparable)y.GetType().GetProperty( comparer).GetValue(y, null);
if ( x.GetType().GetProperty(comparer).PropertyType ==
typeof(System.String) )
{
icx = (IComparable) icx.ToString().ToUpper();
icy = (IComparable) icy.ToString().ToUpper();
}
if(this.sortDirection == SortDirection.Descending)
return icy.CompareTo(icx);
else
return icx.CompareTo(icy);
}
}
public int Compare(object x, object y)
{
return Compare( x, y, sortBy);
}
}
public enum SortByType
{
Method = 0,
Property = 1
}
public enum SortDirection
{
Ascending = 0,
Descending = 1
}
*******************************************************
"CodeRazor" <CodeRazor@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:CDA2CDBE-9ED0-493A-A077-5A3A3C54E850@xxxxxxxxxxxxxxxx
> Hi,
>
> I want to retrieve a sorted list of files, ordered by LastWriteTime. I
> know
> I can implement IComparer, but I don't know how or what this means. I know
> a
> sortedlist has objects and keys and that I could make the key for my
> sorted
> list the LastWriteTime of the file.
> Apart from this i'm at a real loose end.
>
> Any clues anyone?
>
> thank you
.
- Follow-Ups:
- Re: SortedList of Files using IComparer
- From: CodeRazor
- Re: SortedList of Files using IComparer
- References:
- SortedList of Files using IComparer
- From: CodeRazor
- SortedList of Files using IComparer
- Prev by Date: DataReader to Array
- Next by Date: DataTable- retreiving Description Field from schema
- Previous by thread: SortedList of Files using IComparer
- Next by thread: Re: SortedList of Files using IComparer
- Index(es):
Relevant Pages
|