Given the following list: "A", "B", "C", "D", "E", "F", "G"
I need a comparator that does the following sorting:
- specify a certain element (e.g.
"D") - start with the element
- followed by all following elements of the original list in the original order
- followed by all preceding elements of the original list in the original order
The result would be: "D", "E", "F", "G", "A", "B", "C"
Please be aware that I know that I could just do stuff similar to the following:
List<String> following = myList.subList(myList.indexOf("D") + 1, myList.size());
List<String> preceding = myList.subList(0, myList.indexOf("D"));
List<String> newList = Stream.of(Collections.singletonList("D"), following, preceding)
.flatMap(List::stream)
.collect(Collectors.toList());
In this question I explicitly mean a Comparator implementation.
It is clear that it will have to have the list & element as a parameter, I am just not clear about the comparison algorithm itself:
private static class MyComparator<T> implements Comparator<T> {
private final List<T> list;
private final T element;
private MyComparator(List<T> list, T element) {
this.list = list;
this.element = element;
}
@Override
public int compare(T o1, T o2) {
// Not clear
}
}