SortedList of Files using IComparer

G

Guest

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
 
I

Ignacio Machin \( .NET/ C# MVP \)

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
}

*******************************************************
 
G

Guest

cheers again Ignacio, but I found a way of working it.

For those interested:
I created a class that inherits from the IComparer class, put in code to
sort it and then just used Array.Sort(myArray,myComparerClassInstance)
 
I

Ignacio Machin \( .NET/ C# MVP \)

hi,

Well that's precisely how you use the code I sent you :)

A few details though.

IComparer is an interface, not a class, you do not inherit from it, you
implemen it.

The code I sent you do this, but it's generic it does work with a collection
of any type ( or differen types as long as they define a property with the
same name and type )


cheers,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top