nus-cs2103-AY2122S2 / forum

13 stars 1 forks source link

Why filteredlist.sort() method fails. #198

Closed Jacky142857 closed 2 years ago

Jacky142857 commented 2 years ago

This question may nothing to do with the project. So filteredlist implements list and has sort() method built in. However, when I try to apply sort() on filteredlist, it fails. but for observablelist, it works fine.

yusufaine commented 2 years ago

Hi, could you provide a little more detail about filteredlist such as what's the type that you're storing in there?

Jacky142857 commented 2 years ago

Hi, could you provide a little more detail about filteredlist such as what's the type that you're storing in there?

image So the list.sort() works fine but flist.sort() crashes, since they both implement list, it is quite wired to see this situation

yusufaine commented 2 years ago

@Jacky142857 I'm not sure if this is the correct answer but from my testing, it seems like FilteredList is read-only ("truly-immutable", which is why it's initialised the way that it is). The only way to change it is by modifying the input ObservableList as mentioned in the docs:

Wraps an ObservableList and filters its content using the provided Predicate. All changes in the ObservableList are propagated immediately to the FilteredList.

If you still want to sort the FilteredList without the underlying ObservableList, I think you can use the ObservableList::sorted(), and your highlighted line would be something like flist.sorted().

Hope that helps!