Skip to content

Commit 5929b20

Browse files
committed
Dont drop items when mapping a collection to a sorted list
1 parent bb9432a commit 5929b20

File tree

1 file changed

+8
-6
lines changed
  • src/main/java/software/coley/collections

1 file changed

+8
-6
lines changed

src/main/java/software/coley/collections/Lists.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -331,8 +331,10 @@ public static <T extends Comparable<T>, C extends Collection<T>> List<T> sorted(
331331
if (collection instanceof SortedSet)
332332
return new ArrayList<>(collection);
333333

334-
// Wrap implicitly sorted collection
335-
return new ArrayList<>(new TreeSet<>(collection));
334+
// Create new list and sort it
335+
List<T> list = new ArrayList<>(collection);
336+
list.sort(Comparator.naturalOrder());
337+
return list;
336338
}
337339

338340
/**
@@ -357,10 +359,10 @@ public static <T, C extends Collection<T>> List<T> sorted(@Nonnull Comparator<T>
357359
if (collection instanceof SortedSet)
358360
return new ArrayList<>(collection);
359361

360-
// Wrap implicitly sorted collection
361-
TreeSet<T> sortedSet = new TreeSet<>(comparator);
362-
sortedSet.addAll(collection);
363-
return new ArrayList<>(sortedSet);
362+
// Create new list and sort it
363+
List<T> list = new ArrayList<>(collection);
364+
list.sort(comparator);
365+
return list;
364366
}
365367

366368
/**

0 commit comments

Comments
 (0)