[concurrency-interest] need to use collection.sort w. CopyOnWriteArrayList but cant.....

Doug Lea dl@cs.oswego.edu
Tue, 1 Jun 2004 07:42:40 -0400


> i need ot add sort capability and tried doing it through the
> Collections.sort but this fails cause the sort method eventually calls the
> set method on the list iterator and this is not supported in the
> CopyOnWriteArrayList.
> do i have alternatives?


You could use something like (omitting any generic types you might be
using here):

  Object[] array = list.toArray();
  Arrays.sort(array);
  list.clear();
  list.addAll(Arrays.asList(array));

This is not too fast, but at least makes only two full copies
per sort rather than a copy per element change.

-Doug