Sorting of new added items

Hello all.
I’m trying to make new elements added to a Table appear in the right order. So far, I’ve tried:

-To set SortContainerPropertyId to my Table subclass.
-To set an itemSorter into my Container (BeanItemContainer subclass).

But new elements keep being added at the end of the table.

I’m using DefaultItemSorter instantiated on a comparator for String as:

public class CarrierComparator implements Comparator<Object> {
	public int compare(Object o1, Object o2) {
		if (o1 == null) {
			if (o2 == null) {
				return 0;
			} else {
				return -1;
		if (o2 == null) {
			return 1;
		String c1 = (String) o1;
		String c2 = (String) o2;
		return c1.compareTo(c2);

This comparator is not being called when a new Item is added to the container through addItem(Object itemId) so, I suspect I need to make something else.

Best regards.

Going deeper into this problem, I’ve tried two approaches:

  • To override BeanItem addItem(Object itemId) in my BeanItemContainer subclass, as:
	public BeanItem<Carrier> addItem(Object itemId) {
		BeanItem<Carrier> bi = super.addItem(itemId);
		return bi;

Doesn’t look very efficient, to sort the whole collection every time an element is added, but since the collection is always sorted, perhaps it’s not too hard. Unfortunately, this solution doesn’t work, resulting into the following exception:

ERROR: Error in getCarrierList
	at java.util.Arrays.mergeSort(
	at java.util.Arrays.sort(
	at java.util.Collections.sort(
	at es.tid.spci.test.server.CarrierService.fetchCarriers(
	at es.tid.spci.test.server.CarrierService.getData(
	at es.tid.spci.test.ui.CarrierList.<init>(
	at es.tid.spci.test.PoC.getCarrierList(
	at es.tid.spci.test.PoC.getLeftFrame(
	at es.tid.spci.test.PoC.init(
	at com.vaadin.Application.start(
	at com.vaadin.terminal.gwt.server.AbstractApplicationServlet.startApplication(
	at com.vaadin.terminal.gwt.server.AbstractApplicationServlet.service(
	at javax.servlet.http.HttpServlet.service(
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(
	at org.apache.catalina.core.StandardWrapperValve.invoke(
	at org.apache.catalina.core.StandardContextValve.invoke(
	at org.apache.catalina.core.StandardHostValve.invoke(
	at org.apache.catalina.valves.ErrorReportValve.invoke(
	at org.apache.catalina.core.StandardEngineValve.invoke(
	at org.apache.catalina.connector.CoyoteAdapter.service(
	at org.apache.coyote.http11.Http11Processor.process(
	at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(

It fails while adding the second item to the container.

  • The other approach is to calculate the position to insert the new item, something like:
public BeanItem<Carrier> addItem(Object itemId) {
		Carrier thisCarrier = (Carrier)itemId;
		Collection<Carrier> items = this.getItemIds();
		BeanItem<Carrier> bi = null;
		boolean added = false;
		Object previousItem = null;
		for (Carrier item : items) {
			if (item.getName().compareTo(thisCarrier.getName()) > 0) {"Adding " + thisCarrier.getName() 
						+ " before " + item.getName());
				bi = addItemAfter(previousItem, itemId);
				added = true;
			previousItem = item;

		if (!added) {
			bi = super.addItem(itemId);
		return bi;

It gets a little complex, but works.

Any comments, suggestions, about the approach to follow, the error I got…

Best regards.