Sort SortedList by different criteria

Courtesy of sottolounge.nl

SortedList implementation currently supports providing Callback as parameter only in the constructor.

1. What happens when you want change the criteria how SortedList will bubble up/down (champagne bubbles reference) items with different criteria rather than the provided one in the Callback?

2. Is there any way to instruct SortedList to check the criteria when there aren’t changes in the data but changes in the sorting criteria?

Answers:
1. You can’t change the criteria with what’s given. Thanks to uncle OOP, we can work our way, making our Callback to respect sorting criteria only and temporally .

compareTo method is in charge to dictate how list items are ordered, as you can see, we override our top compareTo implementation with new criteria.

2. No, there isn’t a way to signal SortedList that our criteria has changed. It would be great if we could do that. Something inside SortedList, as cool as this:

public void updateCallback(@Nullable Callback<T> callback) {
//case when we dont have items added, but updating callback on empty data set or one item
if (mSize == 0 || mSize == 1 || callback == null) {
mCallback = callback;
return;
}
//case when we have callback previously set, but we assume it defers with the new one
if(mCallback != null){
//take first and second item and call compare, if they return different results it measn the criteria has changed
int oldResult = mCallback.compare(mData[0], mData[1]);
int newResult = callback.compare(mData[0], mData[1]);

if(oldResult != newResult){
//Yay we have different callbacks, lets notify list to reorder itself.
mCallback = callback;
reorderSelf();
}
}
}

I failed to find proper way to notify SortedList that criteria has changed:
- Called recalculatePositionOfItemAt() for each item at index and failed.
- Called updateItem() for each item and failed.

Then I decided to go bruteforce on it, simply removing all items and re-adding them all over again:

mCallback.setComparator(comparator);
mItemsList.beginBatchedUpdates();
List<Item> itemsCopy = new ArrayList<>();
for (int i = 0; i < mItemsList.size(); i++) {
productsCopy.add(mItemsList.get(i));
}
mItemsList.clear();
mItemsList.addAll(itemsCopy);
mItemsList.endBatchedUpdates();
mCallback.setComparator(null);

Note that this does not call onMove() method for your ItemAnimator.

Conclusion

Please star this feature request on b.android.com. We needz this.