How to make a Table respect the order in which I add items to it?

Hi guys,

I’m using the method addItem(Object cells, Object itemId) to add items to a table. The cells are all editable text fields, and I don’t set, manually, any specific container to the table (from the docs, I understand that, in this case, the table is using an IndexedContainer).

First thing is that I’d prefer to preserve approach of populating the table.
Also, I wouldn’t be able to play with bean items because the number of columns is variable: it depends on the number of dimensions of the record that is used as basis to populate each row (this record is composed by a key and a variable list of dimensions … can be 1, but can be 50, can be even more).

What I do is: I build this array of text fields, one per dimension, and pass to the addItem(…) method. Each text field is populated with the value of the corresponding dimension.

Now I want to sort this table. The default sorting used by the Table doesn’t work.
So, what I did is: disable the sorting of the table, add one header click listener per header of the table, recognize the dimension that is clicked, get the list of records that must be used to populate the table, sort it alphabetically based on the clicked dimension (all this is working pretty fine) and iterate on this list, invoking ‘addItem(…)’ to add reach row individually. I expected that the final table contained the rows in the exact order in which I’ve added the items, but it is not working this way. Should it work the way I expect? May this be a bug? Or I am missing something? I mean: backstages, is ‘addItem’ doing something on its own that prevents me to add rows sequentially to the table just I wanted?

I am sure I am invoking addItem() in a sorted list. If I have 3 columns, for example, and I clicked on the header of the third, I am building a list that contains objects that must populate each row, all them sorted alphabetically based on the values of the third dimension. And then I’d expect to see the rows in the table all displayed in a way that the third column is perfectly organized, alphabetically. You see?

I thank you in advance for the support, guys. Any ideas/suggestions? BTW, I’m using Vaadin 7.7.6.