forked from reactiveui/ReactiveUI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOrderedComparer.cs
More file actions
237 lines (213 loc) · 12.3 KB
/
OrderedComparer.cs
File metadata and controls
237 lines (213 loc) · 12.3 KB
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
using System;
using System.Collections.Generic;
namespace ReactiveUI
{
/// <summary>
/// Convienience interface for providing a starting point for chaining comparers.
/// </summary>
public interface IComparerBuilder<T>
{
/// <summary>
/// Creates a derived comparer based on the given parent comparer. The returned comparer will sort elements
/// using the parent comparer first. If the parent considers the values equal elements will be sorted
/// in ascending order based on the values returned by the provided selector. The selector values will be
/// compared using the default comparer for the return type of the selector.
/// </summary>
/// <param name="selector">A function supplying the values for the comparator.</param>
IComparer<T> OrderBy<TValue>(Func<T, TValue> selector);
/// <summary>
/// Creates a derived comparer based on the given parent comparer. The returned comparer will sort elements
/// using the parent comparer first. If the parent considers the values equal elements will be sorted
/// in ascending order based on the values returned by the provided selector. The selector values will be
/// compared using the provided comparer or the default comparer for the return type of the selector if no
/// comparer is specified.
/// </summary>
/// <param name="selector">A function supplying the values for the comparator.</param>
IComparer<T> OrderBy<TValue>(Func<T, TValue> selector, IComparer<TValue> comparer);
/// <summary>
/// Creates a derived comparer based on the given parent comparer. The returned comparer will sort elements
/// using the parent comparer first. If the parent considers the values equal elements will be sorted
/// in descending order based on the values returned by the provided selector. The selector values will be
/// compared using the default comparer for the return type of the selector.
/// </summary>
/// <param name="selector">A function supplying the values for the comparator.</param>
IComparer<T> OrderByDescending<TValue>(Func<T, TValue> selector);
/// <summary>
/// Creates a derived comparer based on the given parent comparer. The returned comparer will sort elements
/// using the parent comparer first. If the parent considers the values equal elements will be sorted
/// in descending order based on the values returned by the provided selector. The selector values will be
/// compared using the provided comparer or the default comparer for the return type of the selector if no
/// comparer is specified.
/// </summary>
/// <param name="selector">A function supplying the values for the comparator.</param>
IComparer<T> OrderByDescending<TValue>(Func<T, TValue> selector, IComparer<TValue> comparer);
}
/// <summary>
/// Convienience class providing a starting point for chaining comparers for anonymous types.
/// </summary>
/// <remarks>
/// If the type you're creating a comparer for is known this class is nothing more than an alias for the generic
/// OrderedComparer. This class can be used to create comparers for anonymous types
/// </remarks>
public static class OrderedComparer
{
private sealed class OrderedComparerTypeWrapper<T> : IComparerBuilder<T>
{
public static readonly OrderedComparerTypeWrapper<T> Instance = new OrderedComparerTypeWrapper<T>();
public IComparer<T> OrderBy<TValue>(Func<T, TValue> selector)
{
return OrderedComparer<T>.OrderBy(selector);
}
public IComparer<T> OrderBy<TValue>(Func<T, TValue> selector, IComparer<TValue> comparer)
{
return OrderedComparer<T>.OrderBy(selector, comparer);
}
public IComparer<T> OrderByDescending<TValue>(Func<T, TValue> selector)
{
return OrderedComparer<T>.OrderByDescending(selector);
}
public IComparer<T> OrderByDescending<TValue>(Func<T, TValue> selector, IComparer<TValue> comparer)
{
return OrderedComparer<T>.OrderByDescending(selector, comparer);
}
}
/// <summary>
/// Creates a type inferred comparer builder for the element type of the enumerable. Useful for creating
/// comparers for anonymous types. Note that the builder is not a comparer in itself, you need to use the
/// OrderBy or OrderByDescending methods on the builder to get an actual comparer.
/// </summary>
public static IComparerBuilder<T> For<T>(IEnumerable<T> enumerable)
{
return For<T>();
}
/// <summary>
/// Creates a comparer builder for the specified type. Note that the builder is not a comparer in itself,
/// you need to use the OrderBy or OrderByDescending methods on the builder to get an actual comparer.
/// If the type is known at compile time this method is nothing more than an alias for the generic
/// OrdedComparer class.
/// </summary>
public static IComparerBuilder<T> For<T>()
{
return OrderedComparerTypeWrapper<T>.Instance;
}
}
/// <summary>
/// Convienience class providing a starting point for chaining comparers.
/// </summary>
/// <typeparam name="T"></typeparam>
public static class OrderedComparer<T>
{
/// <summary>
/// Creates a comparer that will sort elements in ascending order based on the values returned by the provided
/// selector. The values will be compared using the default comparer for the return type of the selector.
/// </summary>
/// <param name="selector">A function supplying the values for the comparator.</param>
public static IComparer<T> OrderBy<TValue>(Func<T,TValue> selector)
{
return ComparerChainingExtensions.ThenBy<T, TValue>(null, selector);
}
/// <summary>
/// Creates a comparer that will sort elements in ascending order based on the values returned by the provided
/// selector. The selector values will be compared using the provided comparer or the default comparer for the
/// return type of the selector if no comparer is specified.
/// </summary>
/// <param name="selector">A function supplying the values for the comparator.</param>
/// <param name="comparer">
/// The comparer to use when comparing the values returned by the selector.
/// The default comparer for that type will be used if this parameter is null.
/// </param>
public static IComparer<T> OrderBy<TValue>(Func<T, TValue> selector, IComparer<TValue> comparer)
{
return ComparerChainingExtensions.ThenBy<T, TValue>(null, selector, comparer);
}
/// <summary>
/// Creates a comparer that will sort elements in descending order based on the values returned by the provided
/// selector. The values will be compared using the default comparer for the return type of the selector.
/// </summary>
/// <param name="selector">A function supplying the values for the comparator.</param>
public static IComparer<T> OrderByDescending<TValue>(Func<T, TValue> selector)
{
return ComparerChainingExtensions.ThenByDescending<T, TValue>(null, selector);
}
/// <summary>
/// Creates a comparer that will sort elements in descending order based on the values returned by the provided
/// selector. The selector values will be compared using the provided comparer or the default comparer for the
/// return type of the selector if no comparer is specified.
/// </summary>
/// <param name="selector">A function supplying the values for the comparator.</param>
/// <param name="comparer">
/// The comparer to use when comparing the values returned by the selector.
/// The default comparer for that type will be used if this parameter is null.
/// </param>
public static IComparer<T> OrderByDescending<TValue>(Func<T, TValue> selector, IComparer<TValue> comparer)
{
return ComparerChainingExtensions.ThenByDescending<T, TValue>(null, selector, comparer);
}
}
public static class ComparerChainingExtensions
{
/// <summary>
/// Creates a derived comparer based on the given parent comparer. The returned comparer will sort elements
/// using the parent comparer first. If the parent considers the values equal elements will be sorted
/// in ascending order based on the values returned by the provided selector. The selector values will be
/// compared using the default comparer for the return type of the selector.
/// </summary>
/// <param name="selector">A function supplying the values for the comparator.</param>
public static IComparer<T> ThenBy<T, TValue>(this IComparer<T> parent, Func<T, TValue> selector)
{
return ThenBy(parent, selector, Comparer<TValue>.Default);
}
/// <summary>
/// Creates a derived comparer based on the given parent comparer. The returned comparer will sort elements
/// using the parent comparer first. If the parent considers the values equal elements will be sorted
/// in ascending order based on the values returned by the provided selector. The selector values will be
/// compared using the provided comparer or the default comparer for the return type of the selector if no
/// comparer is specified.
/// </summary>
/// <param name="selector">A function supplying the values for the comparator.</param>
public static IComparer<T> ThenBy<T, TValue>(this IComparer<T> parent, Func<T, TValue> selector, IComparer<TValue> comparer)
{
return new ChainedComparer<T>(parent, (x, y) => comparer.Compare(selector(x), selector(y)));
}
/// <summary>
/// Creates a derived comparer based on the given parent comparer. The returned comparer will sort elements
/// using the parent comparer first. If the parent considers the values equal elements will be sorted
/// in descending order based on the values returned by the provided selector. The selector values will be
/// compared using the default comparer for the return type of the selector.
/// </summary>
/// <param name="selector">A function supplying the values for the comparator.</param>
public static IComparer<T> ThenByDescending<T, TValue>(this IComparer<T> parent, Func<T, TValue> selector)
{
return ThenByDescending(parent, selector, Comparer<TValue>.Default);
}
/// <summary>
/// Creates a derived comparer based on the given parent comparer. The returned comparer will sort elements
/// using the parent comparer first. If the parent considers the values equal elements will be sorted
/// in descending order based on the values returned by the provided selector. The selector values will be
/// compared using the provided comparer or the default comparer for the return type of the selector if no
/// comparer is specified.
/// </summary>
/// <param name="selector">A function supplying the values for the comparator.</param>
public static IComparer<T> ThenByDescending<T, TValue>(this IComparer<T> parent, Func<T, TValue> selector, IComparer<TValue> comparer)
{
return new ChainedComparer<T>(parent, (x, y) => -comparer.Compare(selector(x), selector(y)));
}
}
internal sealed class ChainedComparer<T> : IComparer<T>
{
private IComparer<T> parent;
private Comparison<T> inner;
public ChainedComparer(IComparer<T> parent, Comparison<T> comparison)
{
if (comparison == null)
throw new ArgumentNullException("comparison");
this.parent = parent;
this.inner = comparison;
}
public int Compare(T x, T y)
{
int parentResult = parent == null ? 0 : parent.Compare(x, y);
return parentResult != 0 ? parentResult : inner(x, y);
}
}
}