CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Nov 2008
    Posts
    4

    Arraylist,IComparer

    Hai Frnds, Iam beginner to dotnet. Please explain mr below in detail.


    class User : IComparable {
    private string firstName;
    private string lastName;

    public User(string fn, string ln) {
    this.firstName=fn;
    this.lastName=ln;
    }
    //I did not understand this below method at all..please be clear here
    public int CompareTo(object obj) {
    User u=(User)obj;
    if (this.lastName==u.lastName) {
    return this.firstName.CompareTo(u.firstName);
    } else {
    return this.lastName.CompareTo(u.lastName);
    }
    }

    public override string ToString() {
    return lastName+" "+firstName;
    }

    Thanking you in advance

  2. #2
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: Arraylist,IComparer

    Code:
    //I did not understand this below method at all..please be clear here
    public int CompareTo(object obj) {
    It simply compares the last and first names.....

    What specifically to you not understand???
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  3. #3
    Join Date
    Nov 2008
    Posts
    4

    Re: Arraylist,IComparer

    Thanks a lot frnd, If you dont mind could you please explain in detail How does IComparer and Icomparable works with arraylist??

  4. #4
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: Arraylist,IComparer

    ICompareble is a simple interface which defines how to compare two objects.

    ArrayList (when used with comparable objects), can be sorted by using this interface.

    I still dont understand what you are SPECIFICALLY looking for....
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured