Wrox Programmer Forums
|
BOOK: Beginning Visual C# 2010
This is the forum to discuss the Wrox book Beginning Visual C# 2010 by Karli Watson, Christian Nagel, Jacob Hammer Pedersen, Jon D. Reid, Morgan Skinner, ; ISBN: 9780470502266
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning Visual C# 2010 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 May 4th, 2012, 09:24 AM
Registered User
 
Join Date: May 2010
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Chapter 11 Sorting Question

In chapter 11, there is a Try It Out section about using Sorting with IComparable.

Below is the line that called the sorting method
Code:
Console.WriteLine("People sorted with default comparer (by age):");
list.Sort();
When I debugged it, that line goes to CompareTo method just like the book explained, but I don't understand how the CompareTo method ended up sorting the ArrayList. I just do not see how this happens even after I debug the code. Can anyone explain why the code below works to sort the ArrayList? I do not see that the code change the position of the items in the ArrayList.

Code:
public int CompareTo(object obj)
    {
      if (obj is Person)
      {
        Person otherPerson = obj as Person;
        return this.Age - otherPerson.Age;
      }
      else
      {
        throw new ArgumentException("Object to compare to is not a Person object.");
      } 
    }





Similar Threads
Thread Thread Starter Forum Replies Last Post
Ch 11 Question tixrus BOOK: Beginning JavaScript and CSS Development with jQuery 0 April 11th, 2010 05:23 PM
Chapter 11: SQL Server connection (General question) StamPita BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 1 January 2nd, 2010 02:02 PM
chapter 11 figure 11-7 relative positioning pelopito BOOK: Beginning CSS: Cascading Style Sheets for Web Design ISBN: 978-0-7645-7642-3 2 November 29th, 2007 06:11 AM
A simple Sorting question bonekrusher XSLT 2 November 25th, 2007 09:40 AM
Quick Question for Author of Chapter 11 Wullie BOOK: Beginning ASP.NET 2.0 BOOK VB ISBN: 978-0-7645-8850-1; C# ISBN: 978-0-470-04258-8 0 July 16th, 2006 06:54 PM





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