Have an entry in a collection always sort to top.

I have a collection of beans, and one of the entries in that collection I always want to appear at the top of the list regardless of what column the user sorts on. Take the following table

type make model
Add New
Coupe Ford Mustang
Sedan Telsa Model S
Sedan Ford Taurus
Wagon Ford Flex
Wagon Subaru Outback

If I was to sort by make or sort the type in decending order, I still want the “Add New” entry to appear at the top of the list. I would guess that I would use an ItemSorter, but I am not sure how I would code it. Any help on this would be appreciated.

Thank you

You should override the compareTo() method in your bean class to handle the “Add New” case the way you want it and then call the compareTo() method of the superclass for the other cases. This link should provide much of the needed information:


http://javarevisited.blogspot.com/2011/11/how-to-override-compareto-method-in.html

Or you can override
sort(Object[] propertyId, boolean[]
ascending)
method in table:

    @Override
    public void sort(Object[] propertyId, boolean[]
 ascending)
            throws UnsupportedOperationException {
       removeItem(addNewItemItem);
       super.sort(propertyId, ascending);
       ((BeanItemContainer<T>) getContainerDataSource()).addItemAt(0, addNewItemItem);
    }

Got the override of the sort method in the table to work. I did have to make some changes to the code above.

[code]
@Override
public void sort(Object propertyId, boolean
ascending)
throws UnsupportedOperationException {
// TODO Auto-generated method stub

        <BeanContainer>.removeItem(addNewItemId);
        
        super.sort(propertyId, ascending);
        
        <BeanContainer>.addBeanAt(0, addNewItemItem);

[/code]Some Notes

  • BeanContainer – refers to the BeanItemContainer that holds your Beans
  • addNewItemId – refers to the value in the Bean that is used to reference it
  • addNewItemItem – refers to the Bean that represents the “**Add New **” entry in the table.




Thank you
Freddy