I'm trying to sort my dynamic columns in a cross tab according to some custom scheme.
In the docs I found mention of comparatorExpression: Crosstab group bucket comparator expression. The result of this expression is used to sort the buckets, in ascending or descending order. If no comparator expression is specified, the natural order will be used.
but I don't understand what the expression should look like. Can I somehow use a regular java comparator? Can someone share an example?
There may be much easier and straight forward solutions:
I am using Jasper Reports Studio (Eclipse Plugin).
did not finally work for me :-( ... (see 1. comment below my answer - may be a bug) you just can check [x] Data Pre Sorted in the Crosstab Properties. Of course this only works if you do not need another ordering somewhere else in the report based on the pre-sorted result set.
using an invisible crosstab group header, which is rather tricky:
Row Group1
or similar as is for nowTotal Position
to None
(we do not want to have a total column per row/column generated)name="invisible sort column ..."
if you are using group totals on your group (Total Position
!= None
) then you have to basically move these totalling elements/settings to the first dummy sort group, because otherwise the totals will be no totals anymore (= per 2nd group totals) displayed after each group column/row, e.g. (here only shown with column group, but row group follows the same principle)
a|b|c|sum a|sum|b|sum|c|sum
========= => =================
1|2|3|6 1|1 |2|2 |3|3
easiest may be to do this in the XML similar to this transformation (do not forget to move the totalPosition=...
and columnTotalGroup=...
attributes and to change the sum if applicable to your scenario $V{SomeSum_..._ALL}
):
...
<columnGroup name="OrderXDummy" height="0">
...
<crosstabTotalColumnHeader>
<cellContents/>
</crosstabTotalColumnHeader>
</columnGroup>
...
<columnGroup name="X" ... totalPosition="End">
...
<crosstabTotalColumnHeader>
<cellContents ...>
...
</cellContents>
</crosstabTotalColumnHeader>
</columnGroup>
...
<crosstabCell ... columnTotalGroup="X">
...
<textFieldExpression><![CDATA[$V{SomeSum_X_ALL}]]></textFieldExpression>
...
</crosstabCell>
=>
...
<columnGroup name="OrderXDummy" height="0" totalPosition="End">
...
<crosstabTotalColumnHeader>
<cellContents ...>
...
</cellContents>
</crosstabTotalColumnHeader>
</columnGroup>
...
<columnGroup name="X" ... >
...
<crosstabTotalColumnHeader>
<cellContents/>
</crosstabTotalColumnHeader>
...
</columnGroup>
...
<crosstabCell ... columnTotalGroup="OrderXDummy">
...
<textFieldExpression><![CDATA[$V{SomeSum_OrderXDummy_ALL}]]></textFieldExpression>
...
</crosstabCell>
(may be skipped:) remove unnecessarily generated <crosstabCell ... column/rowTotalGroup="...">
cells
$F{ORDER_FOR_X}
Value Class Name
to java.lang.Integer
or whatever fits for your values here (have a look at your dataset which type is assigned there if your are using it via some column)add some variable expression to the Order By Expression
of the original group, e.g. $V{ORDER_FOR_X}
$V{...}
is the trick, do not use $F{...}
!meaning if you can provide some field that defines the sort and relates to your to-be-sorted column value, e.g. (Oracle SQL)
select 1 as order_for_x, 'foo' as x, 'bla blu' as y from dual
union all select 2, 'bar', 'ta tu' from dual
union all select 2, 'bar', 'na na' from dual
union all select 1, 'foo', 'check it' from dual
union all select 3, 'queue', 'sap' from dual
otherwise you can of course use something else here as well
if you should get some
...
Caused by: java.lang.NullPointerException
at org.apache.commons.collections.comparators.ComparableComparator.compare(ComparableComparator.java:92)
at net.sf.jasperreports.crosstabs.fill.BucketExpressionOrderer.compareOrderValues(BucketExpressionOrderer.java:70)
...
you can simply change the expression to $V{ORDER_FOR_X} == null ? 0 : $V{ORDER_FOR_X}
which should do the trick
set all high/width fields of the dummy group to 0
and the text fields Print When Expression
to false
using an order by based on measure totals (as described in the bottom link)
using a custom Java Comparator class (as described in the answer from Pieter VN 2011-11-16)
further maybe helpful links I found: