|
21 | 21 | import java.util.List; |
22 | 22 | import java.util.NoSuchElementException; |
23 | 23 | import java.util.SortedMap; |
24 | | -import java.util.SortedSet; |
25 | 24 | import java.util.TreeMap; |
26 | | -import java.util.TreeSet; |
27 | 25 | import java.util.function.Function; |
28 | 26 | import java.util.function.Predicate; |
29 | 27 | import java.util.function.ToDoubleFunction; |
@@ -235,37 +233,58 @@ public static <T, R> List<R> flatten(Iterable<T> iterable, Function<? super T, ? |
235 | 233 | } |
236 | 234 |
|
237 | 235 | /** |
238 | | - * Collects iterable contents. |
| 236 | + * Sorts iterable contents. |
239 | 237 | * |
240 | 238 | * @param <T> |
241 | 239 | * The element type. |
242 | 240 | * |
243 | | - * @param <R> |
244 | | - * The target type. |
| 241 | + * @param <V> |
| 242 | + * The value type. |
245 | 243 | * |
246 | 244 | * @param iterable |
247 | | - * The iterable to collect. |
| 245 | + * The iterable to sort. |
248 | 246 | * |
249 | | - * @param transform |
250 | | - * The transform function. |
| 247 | + * @param identifier |
| 248 | + * The identification function. |
251 | 249 | * |
252 | 250 | * @return |
253 | | - * The collected contents. |
| 251 | + * The sorted contents. |
254 | 252 | */ |
255 | | - public static <T, R extends Comparable<? super R>> SortedSet<R> collect(Iterable<T> iterable, Function<? super T, ? extends Iterable<? extends R>> transform) { |
256 | | - if (iterable == null || transform == null) { |
257 | | - throw new IllegalArgumentException(); |
258 | | - } |
| 253 | + public static <T, V extends Comparable<? super V>> List<T> sortBy(Iterable<T> iterable, Function<? super T, ? extends V> identifier) { |
| 254 | + return sortBy(iterable, identifier, Comparator.naturalOrder()); |
| 255 | + } |
259 | 256 |
|
260 | | - var set = new TreeSet<R>(); |
| 257 | + /** |
| 258 | + * Sorts iterable contents. |
| 259 | + * |
| 260 | + * @param <T> |
| 261 | + * The element type. |
| 262 | + * |
| 263 | + * @param <V> |
| 264 | + * The value type. |
| 265 | + * |
| 266 | + * @param iterable |
| 267 | + * The iterable to sort. |
| 268 | + * |
| 269 | + * @param identifier |
| 270 | + * The identification function. |
| 271 | + * |
| 272 | + * @param comparator |
| 273 | + * The comparator. |
| 274 | + * |
| 275 | + * @return |
| 276 | + * The sorted contents. |
| 277 | + */ |
| 278 | + public static <T, V> List<T> sortBy(Iterable<T> iterable, Function<? super T, ? extends V> identifier, Comparator<? super V> comparator) { |
| 279 | + var list = new ArrayList<T>(); |
261 | 280 |
|
262 | | - for (var elements : mapAll(iterable, transform)) { |
263 | | - for (var element : elements) { |
264 | | - set.add(element); |
265 | | - } |
| 281 | + for (var element : iterable) { |
| 282 | + list.add(element); |
266 | 283 | } |
267 | 284 |
|
268 | | - return set; |
| 285 | + list.sort((t1, t2) -> comparator.compare(identifier.apply(t1), identifier.apply(t2))); |
| 286 | + |
| 287 | + return list; |
269 | 288 | } |
270 | 289 |
|
271 | 290 | /** |
|
0 commit comments