By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
460,028 Members | 829 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 460,028 IT Pros & Developers. It's quick & easy.

SortedList of Files using IComparer

P: n/a
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
Nov 17 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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" <Co*******@discussions.microsoft.com> wrote in message
news:CD**********************************@microsof t.com...
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

Nov 17 '05 #2

P: n/a
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)
Nov 17 '05 #3

P: n/a
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,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"CodeRazor" <Co*******@discussions.microsoft.com> wrote in message
news:09**********************************@microsof t.com...
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)

Nov 17 '05 #4

P: n/a
ah, yes. cheers.
Nov 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.