Comparer<T> Class
IComparer<T> 제네릭 인터페이스의 구현에 대한 기본 클래스를 제공합니다.
[] public abstract class Comparer<T> : System.Collections.Generic.IComparer<T>, System.Collections.IComparer
예제
다음 예제에서는 파생 클래스 BoxLengthFirst
는 Comparer<T> 로부터 상속 받은 클래스입니다.
이 comparer 는 타입 Box 의 두 오브젝트를 비교합니다
먼저 길이에 의해 그다음, 높이, 너비에 의해 정렬됩니다.
Box 클래스는 두 Box 오브젝트들 사이에서 기본 비교를 다루기위해 IComparable<T> 인터페이스를 구현합니다
이 기본 구현은 높이, 길이 그리고 넓이에 의해 정렬됩니다
예제는 차이점을 나타냅니다, 두 비교에서 이 비교는 Box오브젝트 List를 정렬하는 것
첫번째로 BoxLengthFirst Comparer 를 사용하는 것 그리고 기본 comparer 를 사용하여 비교하는 것입니다
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 | using System; using System.Collections; using System.Collections.Generic; class Program { static void Main(string[] args) { List<Box> Boxes = new List<Box>(); Boxes.Add(new Box(4, 20, 14)); Boxes.Add(new Box(12, 12, 12)); Boxes.Add(new Box(8, 20, 10)); Boxes.Add(new Box(6, 10, 2)); Boxes.Add(new Box(2, 8, 4)); Boxes.Add(new Box(2, 6, 8)); Boxes.Add(new Box(4, 12, 20)); Boxes.Add(new Box(18, 10, 4)); Boxes.Add(new Box(24, 4, 18)); Boxes.Add(new Box(10, 4, 16)); Boxes.Add(new Box(10, 2, 10)); Boxes.Add(new Box(6, 18, 2)); Boxes.Add(new Box(8, 12, 4)); Boxes.Add(new Box(12, 10, 8)); Boxes.Add(new Box(14, 6, 6)); Boxes.Add(new Box(16, 6, 16)); Boxes.Add(new Box(2, 8, 12)); Boxes.Add(new Box(4, 24, 8)); Boxes.Add(new Box(8, 6, 20)); Boxes.Add(new Box(18, 18, 12)); // Sort by an Comparer<T> implementation that sorts // first by the length. Boxes.Sort(new BoxLengthFirst()); Console.WriteLine("H - L - W"); Console.WriteLine("=========="); foreach (Box bx in Boxes) { Console.WriteLine("{0}\t{1}\t{2}", bx.Height.ToString(), bx.Length.ToString(), bx.Width.ToString()); } Console.WriteLine(); Console.WriteLine("H - L - W"); Console.WriteLine("=========="); // Get the default comparer that // sorts first by the height. Comparer<Box> defComp = Comparer<Box>.Default; // Calling Boxes.Sort() with no parameter // is the same as calling Boxs.Sort(defComp) // because they are both using the default comparer. Boxes.Sort(); foreach (Box bx in Boxes) { Console.WriteLine("{0}\t{1}\t{2}", bx.Height.ToString(), bx.Length.ToString(), bx.Width.ToString()); } // This explicit interface implementation // compares first by the length. // Returns -1 because the length of BoxA // is less than the length of BoxB. BoxLengthFirst LengthFirst = new BoxLengthFirst(); Comparer<Box> bc = (Comparer<Box>) LengthFirst; Box BoxA = new Box(2, 6, 8); Box BoxB = new Box(10, 12, 14); int x = LengthFirst.Compare(BoxA, BoxB); Console.WriteLine(); Console.WriteLine(x.ToString()); } } public class BoxLengthFirst : Comparer<Box> { // Compares by Length, Height, and Width. public override int Compare(Box x, Box y) { if (x.Length.CompareTo(y.Length) != 0) { return x.Length.CompareTo(y.Length); } else if (x.Height.CompareTo(y.Height) != 0) { return x.Height.CompareTo(y.Height); } else if (x.Width.CompareTo(y.Width) != 0) { return x.Width.CompareTo(y.Width); } else { return 0; } } } // This class is not demonstrated in the Main method // and is provided only to show how to implement // the interface. It is recommended to derive // from Comparer<T> instead of implementing IComparer<T>. public class BoxComp : IComparer<Box> { // Compares by Height, Length, and Width. public int Compare(Box x, Box y) { if (x.Height.CompareTo(y.Height) != 0) { return x.Height.CompareTo(y.Height); } else if (x.Length.CompareTo(y.Length) != 0) { return x.Length.CompareTo(y.Length); } else if (x.Width.CompareTo(y.Width) != 0) { return x.Width.CompareTo(y.Width); } else { return 0; } } } public class Box : IComparable<Box> { public Box(int h, int l, int w) { this.Height = h; this.Length = l; this.Width = w; } public int Height { get; private set; } public int Length { get; private set; } public int Width { get; private set; } public int CompareTo(Box other) { // Compares Height, Length, and Width. if (this.Height.CompareTo(other.Height) != 0) { return this.Height.CompareTo(other.Height); } else if (this.Length.CompareTo(other.Length) != 0) { return this.Length.CompareTo(other.Length); } else if (this.Width.CompareTo(other.Width) != 0) { return this.Width.CompareTo(other.Width); } else { return 0; } } } |
icOMPARER<t> 인터페이스를 상속 받아 구현한 클래스 입니다, 그와 같은 GENERIC 클래스들은 다음과 같은 것들이 있습니다
SortedList<TKey,TValue> and SortedDictionary<TKey,TValue> generic classes.
Remarks
Derive from this class to provide a custom implementation of the IComparer<T> interface for use with collection classes such as the SortedList<TKey,TValue> and SortedDictionary<TKey,TValue> generic classes.
The difference between deriving from the Comparer<T> class and implementing the System.IComparable interface is as follows:
To specify how two objects should be compared by default, implement the System.IComparable interface in your class. This ensures that sort operations will use the default comparison code that you provided.
To define a comparer to use instead of the default comparer, derive from the Comparer<T> class. You can then use this comparer in sort operations that take a comparer as a parameter.
The object returned by the Default property uses the System.IComparable<T> generic interface (IComparable<T>
in C#, IComparable(Of T)
in Visual Basic) to compare two objects. If type T
does not implement the System.IComparable<T> generic interface, the Default property returns a Comparer<T> that uses the System.IComparable interface.
ref : https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.comparer-1?view=netframework-4.7.2