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.Design Patterns
8.Development Class
9.Event
10.File Stream
11.Generics
12.GUI Windows Form
13.Language Basics
14.LINQ
15.Network
16.Office
17.Reflection
18.Regular Expressions
19.Security
20.Services Event
21.Thread
22.Web Services
23.Windows
24.Windows Presentation Foundation
25.XML
26.XML LINQ
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
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.