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?
orderByExpression
The way to order crosstab columns normally is by using the
orderByExpression
Integer
)orderByExpression
in thebucket
comparatorExpression
Set a Comparator (create a class in java es.
MyCustomComparator
that implementsComparator
)Comparable
If you are displaying value from own object (
$F{MyField}
is a user defined object) you can simple implement Comparable to display the order as you like.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
toNone
(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)easiest may be to do this in the XML similar to this transformation (do not forget to move the
totalPosition=...
andcolumnTotalGroup=...
attributes and to change the sum if applicable to your scenario$V{SomeSum_..._ALL}
):=>
(may be skipped:) remove unnecessarily generated
<crosstabCell ... column/rowTotalGroup="...">
cells$F{ORDER_FOR_X}
Value Class Name
tojava.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)
otherwise you can of course use something else here as well
if you should get some
you can simply change the expression to
$V{ORDER_FOR_X} == null ? 0 : $V{ORDER_FOR_X}
which should do the trickset all high/width fields of the dummy group to
0
and the text fieldsPrint When Expression
tofalse
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:
I've had the same issue and did not find any examples or explanation but I found how to do it. I'll explain, so hopefully other people can use this solution.
I've used the code below for jasperreports 3.1.0 and iReport 3.1.4, but I guess it works for pretty much all versions.
First you'll have to make sure that you know wich class you have in the bucket expression for your row/column group. By default this is java.lang.String, but I have a custom class there. For this to work I needed to edit the xml like this for my column group:
to
Obviously this customObj value is a field with the corresponding class, defined in the report itself.
Then you'll need to add a Comparator as a parameter, for example:
Now add such a OVERRIDE_Comparator parameter in the jasperreport, using the java.util.Comparator Parameter Class.
Final step: Put $P{OVERRIDE_Comparator} as the Comparator Expression in the row/column group you needed.
When compiling such a report, the most likely compile error would be casting issues. Jasperreports defaults to java.lang.String, you might need to edit the xml of the report manually to get the correct class at each step.
(I found out this method from some asian site, thankfully the code itself was readable! :-) )