Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 2005 > C# 2005
| 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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
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()

hi,

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

Code:
public class RacerComparer : IComparer<Racer>
{
public enum CompareType
{
Name,
Car
}

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);
break;
case CompareType.Car:
result = x.Car.CompareTo(y.Car);
break;
}
return result;
}
}
Code:
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

 
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
Default

Hi,

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!

Phil
 
Old February 15th, 2007, 01:30 PM
Registered User
 
Join Date: Nov 2006
Location: , , .
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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

no more confusions:D





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





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.