Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > C# and C > C# 2005 > C# 2005
Password Reminder
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
C# 2005 For discussion of Visual C# 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# 2005 section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old February 13th, 2007, 03:51 AM
Registered User
Join Date: Nov 2006
Location: , , .
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default generic List<T> Sort()


im new to csharp and im learning it through the professional c# 2005 book..im little confused with the below code

public class RacerComparer : IComparer<Racer>
public enum CompareType

private CompareType compareType;
public RacerComparer(CompareType compareType)
this.compareType = compareType;

public int Compare(Racer x, Racer y)
int result = 0;
switch (compareType)
case CompareType.Name:
result = x.Name.CompareTo(y.Name);
case CompareType.Car:
result = x.Car.CompareTo(y.Car);
return result;
racers.Sort(new RacerComparer(RacerComparer.CompareType.Car));
and here the comparer method takes 2 parameters of type Racer ..but in the implementation we never pass 2 parameters to this method..and if this sort method is sorting the list<T> why do we provide 2 parameters of that type please help me

  #2 (permalink)  
Old February 14th, 2007, 04:28 PM
Friend of Wrox
Points: 894, Level: 11
Points: 894, Level: 11 Points: 894, Level: 11 Points: 894, Level: 11
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
Join Date: Sep 2005
Location: London, , United Kingdom.
Posts: 166
Thanks: 2
Thanked 33 Times in 33 Posts


Sorting and comparing is often a little confusing to begin with, but if you think about what the Sort method is actually doing it can help.

When you call racers.Sort, the list will think, OK I've got all these Racer objects, and I've got to somehow put them in the right order. Luckily I got passed this RacerComparer object to help me...

Because RacerComparer implements IComparer<Racer>, the list knows that if it passes any TWO Racer objects to RacerComparer.Compare(), it will get told which one should come first. So it goes through all the possible pairs of Racers it contains and passes them to the Compare method. Depending on what gets returned, it will add them into the appropriate place in the list.

In terms of the list<T> part, remember T get replaced with whatever Type of object we are dealing with. If we tried using a RacerComparer with a List<int>, it would be sending numbers to the Compare method, which obviously doesn't make sense. By using IComparer<Racer> the compiler knows we are using the correct sort of object.

Hopefully this hasn't confused you even more!

  #3 (permalink)  
Old February 15th, 2007, 01:30 PM
Registered User
Join Date: Nov 2006
Location: , , .
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts

thanks alot..now im really cleared with that:)..hats off to you..

no more confusions:D


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Loading data into List<T> rvincent C# 2005 8 June 14th, 2007 11:27 AM
How to implement a function return List<T> jdang67 C# 2005 2 January 30th, 2006 11:28 AM
<style> tags in a <body> vs. <div> bcat BOOK: Beginning CSS: Cascading Style Sheets for Web Design ISBN: 978-0-7645-7642-3 1 March 27th, 2005 08:50 AM
<marquee><b>About CHAT App. in PHP4</b></marquee> Ramkrishna PHP How-To 1 September 11th, 2004 07:01 AM
<STRONG> vs <B> and <EM> vs <I> anshul HTML Code Clinic 12 September 1st, 2004 05:22 PM

All times are GMT -4. The time now is 06:51 AM.

Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.