3

How do you sort WritableList? I tried using sort method but It gave me an unsupported operation exception

Sample code:

writableList.sort( (o1, o2) -> o1.getPath().compareTo( o2.getPath() ) );

Exception:

java.lang.UnsupportedOperationException
at org.eclipse.core.databinding.observable.list.ObservableList$2.set(ObservableList.java:250)
at java.util.List.sort(List.java:482)

Iterator's set method from ObservableList:

@Override
public void set(E o) {
    throw new UnsupportedOperationException();
}
Kevin King
  • 557
  • 1
  • 9
  • 25

1 Answers1

0

WritableList implements java.util.List. The reason you are getting this exception is mentioned in Javadoc

UnsupportedOperationException - if the list's list-iterator does not support the set operation

You can see the implementation of WritableList's iterator here

You need to use Collections.sort(writableList);

vinS
  • 1,417
  • 5
  • 24
  • 37
  • 1
    What change does it make if I use the Collections.sort? public static void sort(List list, Comparator super T> c) { list.sort(c); } – Kevin King Dec 21 '17 at 05:19
  • Please read this [question](https://stackoverflow.com/questions/34910841/difference-between-collection-sortlist-and-list-sort). In my opinion your 'WritableList' might not be using Java8. – vinS Dec 21 '17 at 05:52