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# / March 2008

Tip: Looking for answers? Try searching our database.

Sorting my custom class in a List<T>

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
DotNetNewbie - 05 Mar 2008 23:12 GMT
Hi,

I have a class that represents a Menu Tab:

public class MenuTab
{
     public string Name;
     public string Filename;
     public int SortOrder;
}

I then load them in a Generic like:

List<MenuTab> menus = new List<MenuTab>();

menus.Add(tab);
// etc.

**Now I want to be able to sort the elements just in case things get
added int he wrong order.

menus.Sort();

How can I make the Collection sort using the SortOrder property of the
MenuTab class?
Jon Skeet [C# MVP] - 05 Mar 2008 23:26 GMT
> I have a class that represents a Menu Tab:
>
[quoted text clipped - 4 lines]
>       public int SortOrder;
> }

Really with public fields? Can't say I like the look of that much...
anyway, moving on to your actual question :)

> I then load them in a Generic like:
>
[quoted text clipped - 10 lines]
> How can I make the Collection sort using the SortOrder property of the
> MenuTab class?

Is it fair to assume that the SortOrder numbers will be *reasonably*
small (so that taking one from another will always give a sane result)?
If so:

C# 2:
menus.Sort(delegate(MenuTab mt1, MenuTab mt2)
   { return mt1.SortOrder-mt2.SortOrder; }
);

C# 3:
menus.Sort( (mt1, mt2) => mt1.SortOrder-mt2.SortOrder );

(Otherwise you should call mt1.SortOrder.CompareTo(mt2.SortOrder)) but
that's a bit longer than it really needs to be.)

Alternatively, in C# 3:

menus = menus.OrderBy(tab => tab.SortOrder).ToList();

Note that that creates a *new* list though - so if you've got anything
else with a reference to the old list, it won't be updated.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk


Rate this thread:







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.