Re: SortedList of Files using IComparer

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



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


.



Relevant Pages

  • Re: Sorting Algorithm
    ... public string Valueset} ... public int Numberset} ... protected SortByType sortByType; ... protected SortDirection sortDirection; ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Sorting DataGrid bound to collection
    ... "Pete Davis" wrote in message ... >>> public LocationPointCollection Sort(string sortParam, SortDirection ... >>> protected SortByType sortByType; ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Collections and ordering them
    ... Find below an example of a generic collection sorter, ... public CtpRecordCollection Sort(string sortParam, CTPCore.SortDirection ... protected SortByType sortByType; ... protected SortDirection sortDirection; ...
    (microsoft.public.dotnet.framework)
  • Re: Sorting DataGrid bound to collection
    ... based on the column header that was clicked, sorted, and re-bound the grid. ... >> public ClassSorter(string sortBy, SortByType sortByType, SortDirection ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Sorting DataGrid bound to collection
    ... Use the class below to sort a collection, ... protected SortByType sortByType; ...
    (microsoft.public.dotnet.languages.csharp)