???? ID: 320727 - ????? ???????: 04 ?????? 2010 - ??????: 2.0

IComparable ?? IComparer ??????? ??? ????? C# ?? ????? ???? ????

?????? ??????This article applies to a different operating system than the one you are using. Article content that may not be relevant to you is disabled.

?? ????? ??

??? ?? ??????? ???? | ??? ?? ??????? ????

??????

?? ??? ?? ??? ???? ????? ???? ?? ?? ?? ??????? ?? ????? ????:IComparer, ??IComparable. ?? ?????? ?? ?? ???????? discussed ???? ???? ??? ???? ??????? ?? ?? ??? ????? ????? ???, ?? ???????? ???? ??? (?? ???? ??? ???? ???), ??????? ?? ??????? ?????????? serve.

??? ???? ??? ?????? ?? ??? ???? (??????????????,????????) ?? ???? ?? ?? ??????IComparer, ?? ??? ?????? ?????? ???? ?? ?????? ??? ???? ?? ???? ????? ?? ???? ???IComparer. ???? ?????? ???, ???? ?? ???? ?? ??????? ??????????? ?? ??? cast ???IComparer(Comparer.Default) ?? ??? ???? ???????, ??? ?? ???? ????? ?????????? ?? ??? ???????? ?? ????? ?? ?????? ?? ???? ????? ???, ?? ???? ????? ??????????? ?? ?? ?? ???????? ?? ?? ??????

?????????? .NET Framework ????? ????????? ???????? ?? ?? ???? ??? ???????? ??:
System.Collections

IComparable

?? ??????IComparable?? ?????????? ?? ???? ????? ?????? ?? ????? ??? ?? ?? ?????? ?????? ???? ?? ??? ??? ??? ?? ???? ???????? ?? ??? ??? ?? ordering ?????? ?? ???? ????? ??? ?? ?????? ??? ?? ????? ??? ??IComparable???? ?????????? ?? ??? ??????? ????? ???? ?? ?????? ???? ?? ??? ???? ?????? ?? ???, ??? ???? ??? ???? ?????? ?? ?????????? ?? ???? ??, ?? ???? ???????? ????????, ?? ?? ????IComparable???????? ?? ????? ?? ????? ???? ?? ????? ?????? ????? ??? ?? ?? ???????????IComparable??????? ?? ???, ???? ?????? ???????????CompareTo????, follows ?? ??? ???:
// Implement IComparable CompareTo method - provide default sort order.
int IComparable.CompareTo(object obj)
{
   car c=(car)obj;
   return String.Compare(this.make,c.make);

}
				
???? ??? ????? ?? ????? ?? ?? ??? ?? ?? ??? ???? ?????? ?? ???? ?? ????? ???String.Compare????? ???? ???? ?? ?? ?????? ??? ??????? ??? ????? ?? ??? ???? ?? ?? ?? ???????? ???

IComparer

?? ??????IComparer???????? ????? ??????? ?????? ????? ??? ?????? ?? ???, ?? ???? ?? ?? ??? ??????? ?? ???, ?? ???? ???? ?? ???????? ?????? ???? ?? ??? ????? ?? ?????? ???? ?? ???? ?????, ?? ????? ???

?? ????? ????IComparer??? two-step ????????? ??? ????, ???? ???? ?? ?? ?? ???? ?????IComparer, ?? ???? ??? ??????????? ?????? ????? ????????:
private class sortYearAscendingHelper : IComparer
{
   int IComparer.Compare(object a, object b)
   {
      car c1=(car)a;
      car c2=(car)b;
      if (c1.year > c2.year)
         return 1;
      if (c1.year < c2.year)
         return -1;
      else
         return 0;
   }
}
				
?? ???IComparer.Compare?????? ?? ?? tertiary ????? ?? ???????? ??? 1, 0, ?? -1 ?? ???? ?? ?? ???? ?? ??? ?? ?? ????, ????? ???? ?? ???, ?? ???? ?? ?? ???? ??? ??? The sort order (ascending or descending) can be changed by switching the logical operators in this method.

The second step is to declare a method that returns an instance of yourIComparer????????:
public static IComparer sortYearAscending()
{      
   return (IComparer) new sortYearAscendingHelper();
}
				
In this example, the object is used as the second argument when you call the overloadedArray.Sortmethod that acceptsIComparer. The use ofICompareris not limited to arrays. It is accepted as an argument in a number of different collection and control classes.

Step-by-Step ??????

The following example demonstrates the use of these interfaces. To demonstrateIComparer, ??IComparable, a class namedcaris created. Thecarobject has themake, ?????????. ?? ??? ??? ????? ????? ?????????????? ?? ?????? ?? ????? ??IComparable??????? ?? ?? ??? descending ????? ?????????????? ?? ?????? ?? ????? ??IComparer???????? ??? ????? ????? ?? ????? ?????? ?? ??? ?????? ????????? ????? ?? ???IComparer.
  1. ??? ????? C#, ??? ??? ????? ????????? ????????? ?????? ????????? ConsoleEnum ??? ???
  2. Host.cs ?? ??? ??? Program.cs ?? ??? ?????, ?? ???? ??? ????? ??? ?? ??? ?? ???????????? ?????

    ???:Visual Studio .NET 2003, Host.cs ?? ??? ??? Class1.cs ?? ??? ??????
    using System;
    
    namespace ConsoleEnum
    {
       class host
       {
          [STAThread]
          static void Main(string[] args)
          {
             // Create an arary of car objects.      
             car[] arrayOfCars= new car[6]
             {
                new car("Ford",1992),
                new car("Fiat",1988),
                new car("Buick",1932),
                new car("Ford",1932),
                new car("Dodge",1999),
                new car("Honda",1977)
             };
          
             // Write out a header for the output.
             Console.WriteLine("Array - Unsorted\n");
    
             foreach(car c in arrayOfCars)
                Console.WriteLine(c.Make + "\t\t" + c.Year);
          
             // Demo IComparable by sorting array with "default" sort order.
             Array.Sort(arrayOfCars);
             Console.WriteLine("\nArray - Sorted by Make (Ascending - IComparable)\n");
    
             foreach(car c in arrayOfCars)
                Console.WriteLine(c.Make + "\t\t" + c.Year);
    
             // Demo ascending sort of numeric value with IComparer.
             Array.Sort(arrayOfCars,car.sortYearAscending());
             Console.WriteLine("\nArray - Sorted by Year (Ascending - IComparer)\n");
    
             foreach(car c in arrayOfCars)
                Console.WriteLine(c.Make + "\t\t" + c.Year);
    
             // Demo descending sort of string value with IComparer.
             Array.Sort(arrayOfCars,car.sortMakeDescending());
             Console.WriteLine("\nArray - Sorted by Make (Descending - IComparer)\n");
    
             foreach(car c in arrayOfCars)
                Console.WriteLine(c.Make + "\t\t" + c.Year);
    
             // Demo descending sort of numeric value using IComparer.
             Array.Sort(arrayOfCars,car.sortYearDescending());
             Console.WriteLine("\nArray - Sorted by Year (Descending - IComparer)\n");
    
             foreach(car c in arrayOfCars)
                Console.WriteLine(c.Make + "\t\t" + c.Year);
         
             Console.ReadLine();
          }
       }
    }
    					
  3. ????????? ?? ??? ?? ????? ??????? ???? ?? ???Car.
  4. ????? ??? ?? Car.cs ??? ??? ?? ???????????? ????:
    using System;
    using System.Collections;
    namespace ConsoleEnum
    {   
       public class car : IComparable
       {      
          // Beginning of nested classes.
    
          // Nested class to do ascending sort on year property.
          private class sortYearAscendingHelper: IComparer
          {
             int IComparer.Compare(object a, object b)
             {
                car c1=(car)a;
                car c2=(car)b;
    
                if (c1.year > c2.year)
                   return 1;
    
                if (c1.year < c2.year)
                   return -1;
    
                else
                   return 0;
             }
          }
    
          // Nested class to do descending sort on year property.
          private class sortYearDescendingHelper: IComparer
          {
             int IComparer.Compare(object a, object b)
             {
                car c1=(car)a;
                car c2=(car)b;
    
                if (c1.year < c2.year)
                   return 1;
    
                if (c1.year > c2.year)
                   return -1;
    
                else
                   return 0;
             }
          }
    
          // Nested class to do descending sort on make property.
          private class sortMakeDescendingHelper: IComparer
          {
             int IComparer.Compare(object a, object b)
             {
                car c1=(car)a;
                car c2=(car)b;
                 return String.Compare(c2.make,c1.make);
             }
          }
    
          // End of nested classes.
    
          private int year;
          private string make;
            
          public car(string Make,int Year)
          {
             make=Make;
             year=Year;
          }
    
          public int Year
          {
             get  {return year;}
             set {year=value;}
          }
    
          public string Make
          {
             get {return make;}
             set {make=value;}
          }
    
          // Implement IComparable CompareTo to provide default sort order.
          int IComparable.CompareTo(object obj)
          {
             car c=(car)obj;
             return String.Compare(this.make,c.make);
          }
    
          // Method to return IComparer object for sort helper.
          public static IComparer sortYearAscending()
          {      
             return (IComparer) new sortYearAscendingHelper();
          }
    
          // Method to return IComparer object for sort helper.
          public static IComparer sortYearDescending()
          {      
             return (IComparer) new sortYearDescendingHelper();
          }
    
          // Method to return IComparer object for sort helper.
          public static IComparer sortMakeDescending()
          {      
            return (IComparer) new sortMakeDescendingHelper();
          }
    
       }
    }
    					
  5. ????????? ?? ?????? ????? ?????? ?????? ????? ??? ????? ???? ??:
    Array - Unsorted
    
    Ford            1992
    Fiat            1988
    Buick           1932
    Ford            1932
    Dodge           1999
    Honda           1977
    
    Array - Sorted by Make (Ascending - IComparable)
    
    Buick           1932
    Dodge           1999
    Fiat            1988
    Ford            1932
    Ford            1992
    Honda           1977
    
    Array - Sorted by Year (Ascending - IComparer)
    
    Ford            1932
    Buick           1932
    Honda           1977
    Fiat            1988
    Ford            1992
    Dodge           1999
    
    Array - Sorted by Make (Descending - IComparer)
    
    Honda           1977
    Ford            1932
    Ford            1992
    Fiat            1988
    Dodge           1999
    Buick           1932
    
    Array - Sorted by Year (Descending - IComparer)
    
    Dodge           1999
    Ford            1992
    Fiat            1988
    Honda           1977
    Buick           1932
    Ford            1932
    					

???? ???? ???? ??:
  • Microsoft Visual C# 2008 Express Edition
  • Microsoft Visual C# 2005 Express Edition
??????: 
kbsweptvs2008 kbhowtomaster kbmt KB320727 KbMthi
???? ?????? ???????????? ?????? ????????
??????????: ?? ???? ?? ???? ??????? ?? ????? ?? Microsoft ????-?????? ?????????? ?????? ?????? ???? ??? ??. Microsoft ???? ??? ????-???????? ?? ????-???????? ????? ?????? ?? ???? ???????? ???? ?? ???? ????? ????? ??? ?? ??? ?????? ?? ???? ???? ???? ??? ????? ??. ???????, ????-???????? ???? ????? ???? ???? ???? ???. ?????, ????????, ?????-???? ?? ??????? ?? ???????? ?? ???? ???, ???? ?? ??? ?????? ???? ???? ??? ????? ??? ?? ???? ??. Microsoft ??????? ??? ???? ?? ?????? ?? ??????????, ????????? ?? ??? ?????? ?? ???? ????? ?? ???? ???????? ?? ??? ???? ????? ?? ??? ????????? ???? ??. Microsoft ????-?????? ?????????? ?? ????? ?????? ?? ?? ??? ??.
?????????? ?? ??????? ????????? ??????? ??:320727  (http://support.microsoft.com/kb/320727/en-us/ )