If I have
class Student
{
public string LastName
public string FirstName
public int Age;
public int ID;
}
and I have an ArrayList that's storing only Student objects, how can I
sort the ArrayList by LastName? Using a IComparer class appears to only
allow < > comparisons on a numeric field...
Dale - 19 Nov 2006 03:31 GMT
Use IComparable.
Or even simpler, here's an article that will show you how to sort on
anything... or everything...
http://ianfnelson.com/blog/archive/2006/02/27/UniversalComparer.aspx
Dale

Signature
Dale Preston
MCAD C#
MCSE, MCDBA
> If I have
>
[quoted text clipped - 9 lines]
> sort the ArrayList by LastName? Using a IComparer class appears to only
> allow < > comparisons on a numeric field...
Cor Ligthert [MVP] - 19 Nov 2006 04:58 GMT
JDeats,
You investigated already the power of the DataTable Class, it is completely
build in, so very cheap to use and can used to be inherited and than
completely strongly typed.
Cor
> If I have
>
[quoted text clipped - 9 lines]
> sort the ArrayList by LastName? Using a IComparer class appears to only
> allow < > comparisons on a numeric field...
Jon Skeet [C# MVP] - 19 Nov 2006 08:36 GMT
> If I have
>
[quoted text clipped - 9 lines]
> sort the ArrayList by LastName? Using a IComparer class appears to only
> allow < > comparisons on a numeric field...
No, IComparer is the way to go here - just return the value of
x.LastName.CompareTo(y.LastName);

Signature
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Dale - 19 Nov 2006 21:08 GMT
Actually, CompareTo() is defined in IComparable. IComparer defines Compare().

Signature
Dale Preston
MCAD C#
MCSE, MCDBA
> > If I have
> >
[quoted text clipped - 12 lines]
> No, IComparer is the way to go here - just return the value of
> x.LastName.CompareTo(y.LastName);
Jon Skeet [C# MVP] - 19 Nov 2006 21:15 GMT
> Actually, CompareTo() is defined in IComparable. IComparer defines
> Compare().
Indeed - but I was suggesting calling String.CompareTo when
implementing Compare.

Signature
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Otis Mukinfus - 19 Nov 2006 17:02 GMT
>If I have
>
[quoted text clipped - 9 lines]
>sort the ArrayList by LastName? Using a IComparer class appears to only
>allow < > comparisons on a numeric field...
Have a look at this:
http://www.otismukinfus.com/vault/vault.htm
Good luck with your project,
Otis Mukinfus
http://www.arltex.com
http://www.tomchilders.com