Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / November 2006

Tip: Looking for answers? Try searching our database.

ArrayList sorting on stronged typed objects

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
JDeats - 19 Nov 2006 03:17 GMT
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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.