implements the IComparable interface : Compare « Collections Data Structure « C# / C Sharp

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Date Time
8.Design Patterns
9.Development Class
10.Event
11.File Stream
12.Generics
13.GUI Windows Form
14.Internationalization I18N
15.Language Basics
16.LINQ
17.Network
18.Office
19.Reflection
20.Regular Expressions
21.Security
22.Services Event
23.Thread
24.Web Services
25.Windows
26.Windows Presentation Foundation
27.XML
28.XML LINQ
C# Book
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source
C# / C Sharp » Collections Data Structure » CompareScreenshots 
implements the IComparable interface
implements the IComparable interface


using System;

public class NewOrderedName : IComparable {
  private String firstName;
  private String lastName;
  public NewOrderedName(String f, String l) {
     firstName = f;
     lastName = l;
  }

  public int CompareTo(Object o) {
    NewOrderedName name = (NewOrderedName)o;
    int lastResult = lastName.CompareTo(name.lastName);
    if (lastResult != 0)
      return lastResult;
    else {
      int firstResult = firstName.CompareTo(name.firstName);
      if (firstResult != 0)
         return firstResult;
      else
         return 1;
    }
  }
  public static void Main() {
    NewOrderedName jAdams = new NewOrderedName("J""A");
    NewOrderedName jqAdams = new NewOrderedName("A""Q");
    NewOrderedName hAdams = new NewOrderedName("H""S");
    Console.WriteLine ("jAdams vs. jqAdams {0}", jAdams.CompareTo(jqAdams))
    Console.WriteLine ("jAdams vs. hAdams {0}", jAdams.CompareTo(hAdams))
    Console.WriteLine ("hAdams vs. hAdams {0}", hAdams.CompareTo(hAdams))
  }
}
           
       
Related examples in the same category
1.Use IComparerUse IComparer
2.Implement IComparableImplement IComparable
3.Sorting and Searching:Advanced Use of HashesSorting and Searching:Advanced Use of Hashes
4.Sorting and Searching:Using IComparerSorting and Searching:Using IComparer
5.Sorting and Searching:Implementing IComparableSorting and Searching:Implementing IComparable
6.Sorting and Searching:IComparer as a PropertySorting and Searching:IComparer as a Property
java2s.com  |  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.