Page 1 of 1

Sort Tables using docx4j

PostPosted: Tue Jul 30, 2013 6:06 pm
by geouser123
Hello experts,

I have a scenario wherein I'm able to create a table in word doc from java using docx4j.
But further my requirement is to sort the table contents first by column 1 and then by column 2 as key.
Any help from your side is deeply appreciated.
Thanking you in advance.

Re: Sort Tables using docx4j

PostPosted: Tue Jul 30, 2013 8:05 pm
by jason
Sounds like you need a Comparator on table row.

See for example http://stackoverflow.com/questions/6907 ... s-list-set

Re: Sort Tables using docx4j

PostPosted: Tue Jul 30, 2013 9:32 pm
by geouser123
Thanks for the quick reply Jason. But the post you have mentioned explains how to sort a list using some GenericComparator. In my case the requirement is a bit different. Whenever we select a table in msword doc, we can sort using the sort option available. Aren't there any options available in docx4j framework itself to get the Tbl object and sort using some specific columns as keys?

Re: Sort Tables using docx4j

PostPosted: Tue Jul 30, 2013 11:28 pm
by jason
geouser123 wrote:Aren't there any options available in docx4j framework itself to get the Tbl object and sort using some specific columns as keys?


No, this is the first time this requirement has come up.

So you need to get the row content as a list (basically, tbl.getContent() ), and define your own Comparator, which in your case works on the cell content.