Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,9 @@ public class CollectionUtils {

private CollectionUtils() {}

@SuppressWarnings({"unchecked", "rawtypes"})
public static <T> List<T> sort(List<T> list) {
public static <T extends Comparable<? super T>> List<T> sort(List<T> list) {
if (isNotEmpty(list)) {
Collections.sort((List) list);
Collections.sort(list);
}
return list;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,4 +244,39 @@ void testOfSet() {
expectedSet.add("C");
assertEquals(expectedSet, set);
}

@Test
void sortShouldSupportComparableSuperType() {
List<Student> students = new ArrayList<>(Arrays.asList(new Student("b"), new Student("a")));

List<Student> sorted = CollectionUtils.sort(students);

assertEquals("a", sorted.get(0).getName());
assertEquals("b", sorted.get(1).getName());
}

private static class Person implements Comparable<Person> {

private final String name;

private Person(String name) {
this.name = name;
}

public String getName() {
return this.name;
}

@Override
public int compareTo(Person other) {
return this.name.compareTo(other.name);
}
}

private static class Student extends Person {

private Student(String name) {
super(name);
}
}
}
Loading