@@ -93,7 +93,7 @@ public static (long DuplicateCount, long Count) GetDuplicateItems<T>([DisallowNu
93
93
}
94
94
return ( nonUniqueCount , allCount ) ;
95
95
}
96
- public static bool IsSortedAscending < T > ( [ DisallowNull ] this IEnumerable < T > collection ) where T : IComparable < T >
96
+ public static bool IsSortedAscending < T > ( [ DisallowNull ] this IEnumerable < T > collection , IComparer < T > comparer )
97
97
{
98
98
if ( collection == null || ! collection . Any ( ) )
99
99
{
@@ -104,7 +104,7 @@ public static bool IsSortedAscending<T>([DisallowNull] this IEnumerable<T> colle
104
104
105
105
foreach ( T item in collection . Skip ( 1 ) )
106
106
{
107
- if ( previous . CompareTo ( item ) >= 0 )
107
+ if ( comparer . Compare ( previous , item ) >= 0 )
108
108
{
109
109
return false ;
110
110
}
@@ -113,7 +113,30 @@ public static bool IsSortedAscending<T>([DisallowNull] this IEnumerable<T> colle
113
113
114
114
return true ;
115
115
}
116
- public static bool IsSortedDescending < T > ( [ DisallowNull ] this IEnumerable < T > collection ) where T : IComparable < T >
116
+ public static bool IsSortedAscending < T > ( [ DisallowNull ] this IEnumerable < T > collection )
117
+ {
118
+ var comparer = Comparer < T > . Default ;
119
+ var enumerator = collection . GetEnumerator ( ) ;
120
+
121
+ if ( ! enumerator . MoveNext ( ) )
122
+ {
123
+ return true ;
124
+ }
125
+
126
+ T previous = enumerator . Current ;
127
+
128
+ while ( enumerator . MoveNext ( ) )
129
+ {
130
+ if ( comparer . Compare ( previous , enumerator . Current ) >= 0 )
131
+ {
132
+ return false ;
133
+ }
134
+ previous = enumerator . Current ;
135
+ }
136
+
137
+ return true ;
138
+ }
139
+ public static bool IsSortedDescending < T > ( [ DisallowNull ] this IEnumerable < T > collection , IComparer < T > comparer )
117
140
{
118
141
if ( collection == null || ! collection . Any ( ) )
119
142
{
@@ -124,7 +147,7 @@ public static bool IsSortedDescending<T>([DisallowNull] this IEnumerable<T> coll
124
147
125
148
foreach ( T item in collection . Skip ( 1 ) )
126
149
{
127
- if ( previous . CompareTo ( item ) <= 0 )
150
+ if ( comparer . Compare ( previous , item ) <= 0 )
128
151
{
129
152
return false ;
130
153
}
@@ -133,7 +156,30 @@ public static bool IsSortedDescending<T>([DisallowNull] this IEnumerable<T> coll
133
156
134
157
return true ;
135
158
}
136
- public static bool IsSortedAscendingAndUnique < T > ( [ DisallowNull ] this IEnumerable < T > collection ) where T : IComparable < T >
159
+ public static bool IsSortedDescending < T > ( [ DisallowNull ] this IEnumerable < T > collection )
160
+ {
161
+ var comparer = Comparer < T > . Default ;
162
+ var enumerator = collection . GetEnumerator ( ) ;
163
+
164
+ if ( ! enumerator . MoveNext ( ) )
165
+ {
166
+ return true ;
167
+ }
168
+
169
+ T previous = enumerator . Current ;
170
+
171
+ while ( enumerator . MoveNext ( ) )
172
+ {
173
+ if ( comparer . Compare ( previous , enumerator . Current ) <= 0 )
174
+ {
175
+ return false ;
176
+ }
177
+ previous = enumerator . Current ;
178
+ }
179
+
180
+ return true ;
181
+ }
182
+ public static bool IsSortedAscendingAndUnique < T > ( [ DisallowNull ] this IEnumerable < T > collection , IComparer < T > comparer )
137
183
{
138
184
if ( collection == null || ! collection . Any ( ) )
139
185
{
@@ -144,7 +190,7 @@ public static bool IsSortedAscendingAndUnique<T>([DisallowNull] this IEnumerable
144
190
145
191
foreach ( T item in collection . Skip ( 1 ) )
146
192
{
147
- if ( previous . CompareTo ( item ) > 0 )
193
+ if ( comparer . Compare ( previous , item ) > 0 )
148
194
{
149
195
return false ;
150
196
}
@@ -153,7 +199,30 @@ public static bool IsSortedAscendingAndUnique<T>([DisallowNull] this IEnumerable
153
199
154
200
return true ;
155
201
}
156
- public static bool IsSortedDescendingAndUnique < T > ( [ DisallowNull ] this IEnumerable < T > collection ) where T : IComparable < T >
202
+ public static bool IsSortedAscendingAndUnique < T > ( [ DisallowNull ] this IEnumerable < T > collection )
203
+ {
204
+ var comparer = Comparer < T > . Default ;
205
+ var enumerator = collection . GetEnumerator ( ) ;
206
+
207
+ if ( ! enumerator . MoveNext ( ) )
208
+ {
209
+ return true ;
210
+ }
211
+
212
+ T previous = enumerator . Current ;
213
+
214
+ while ( enumerator . MoveNext ( ) )
215
+ {
216
+ if ( comparer . Compare ( previous , enumerator . Current ) > 0 )
217
+ {
218
+ return false ;
219
+ }
220
+ previous = enumerator . Current ;
221
+ }
222
+
223
+ return true ;
224
+ }
225
+ public static bool IsSortedDescendingAndUnique < T > ( [ DisallowNull ] this IEnumerable < T > collection , IComparer < T > comparer )
157
226
{
158
227
if ( collection == null || ! collection . Any ( ) )
159
228
{
@@ -164,7 +233,7 @@ public static bool IsSortedDescendingAndUnique<T>([DisallowNull] this IEnumerabl
164
233
165
234
foreach ( T item in collection . Skip ( 1 ) )
166
235
{
167
- if ( previous . CompareTo ( item ) < 0 )
236
+ if ( comparer . Compare ( previous , item ) < 0 )
168
237
{
169
238
return false ;
170
239
}
@@ -173,6 +242,29 @@ public static bool IsSortedDescendingAndUnique<T>([DisallowNull] this IEnumerabl
173
242
174
243
return true ;
175
244
}
245
+ public static bool IsSortedDescendingAndUnique < T > ( [ DisallowNull ] this IEnumerable < T > collection )
246
+ {
247
+ var comparer = Comparer < T > . Default ;
248
+ var enumerator = collection . GetEnumerator ( ) ;
249
+
250
+ if ( ! enumerator . MoveNext ( ) )
251
+ {
252
+ return true ;
253
+ }
254
+
255
+ T previous = enumerator . Current ;
256
+
257
+ while ( enumerator . MoveNext ( ) )
258
+ {
259
+ if ( comparer . Compare ( previous , enumerator . Current ) < 0 )
260
+ {
261
+ return false ;
262
+ }
263
+ previous = enumerator . Current ;
264
+ }
265
+
266
+ return true ;
267
+ }
176
268
public static bool IsSubset < T > ( [ DisallowNull ] this IEnumerable < T > subset , IEnumerable < T > superset )
177
269
{
178
270
if ( subset == null || superset == null )
@@ -193,7 +285,7 @@ public static IDictionary<TKey, TValue> MergeWith<TKey, TValue>([DisallowNull] t
193
285
194
286
foreach ( var kvp in second )
195
287
{
196
- result [ kvp . Key ] = kvp . Value ;
288
+ result [ kvp . Key ] = kvp . Value ;
197
289
}
198
290
199
291
return result ;
0 commit comments