Billy Hope Billy Hope - 1 month ago 5x
Java Question

how to sort columns in ireport crosstab in a specific scheme

The following image shows my crosstab in ireport:


I want to sort my columns in a specific order, knowing that in the crosstab data, i put a list of objects

with the following attributes :

public class ChampEtatAteliers {

private String numero;
private String denominationFr;
private String etatEntreeSortie;

public ChampEtatAteliers(String numero, String denominationFr, String etatEntreeSortie) {
this.numero = numero;
this.denominationFr = denominationFr;
this.etatEntreeSortie = etatEntreeSortie;

is shown in the columns, the
is shown in the rows, and
is used for the count in crosstab

String rapportLien = "/SituationEquipement.jasper";
Map parametres = new HashMap();
JRBeanCollectionDataSource data = new JRBeanCollectionDataSource(atelierses);

in java I'm putting these objects in list
, so is there any way to sort the columns in the list or in the crosstab ireport, my xml code on ireport is as follows:

<columnGroup name="nbreVehicule" height="66">
<bucket order="Descending" class="java.lang.String">
<cellContents backcolor="#F0F8FF" mode="Opaque">
<reportElement style="Crosstab Data Text" x="0" y="0" width="85" height="66" uuid="2109b261-1b20-493a-b664-ce59731c4a20"/>
<box topPadding="1" leftPadding="1" bottomPadding="1" rightPadding="1"/>
<font isBold="true"/>
<measure name="MyOrderByField_measure" class="java.lang.Integer" calculation="Nothing" >


In jasper report the crosstab columnGroup can be sorted ascending or descending by setting the order attribute on the bucket


<columnGroup name="GROUP" height="16" headerPosition="Center">
    <bucket order="Descending" class="java.lang.String">

If you need a user defined order, you should use the orderByExpression

  1. Define a measure for on what you like to order

    <measure name="myOrderByField_measure" class="java.lang.Integer" calculation="First">
  2. Use the measure in orderByExpression

    <columnGroup name="GROUP" height="16" headerPosition="Center">
      <bucket class="java.lang.String">

In your example this could be achieved by introducing a new field in the ChampEtatAteliers class example private int myOrderByField; and then via code setting it as needed.

Another solution, would be to return an own Object (class) as MyField and implement Comparable, that generates the desired order.

EDIT: WORK AROUND, OP CAN NOT MAKE THE orderByExpression work correctly.

Since the myOrderByField is unique (related 1 to 1) to the nbreVehicule he can use myOrderByField in the bucketExpression

<measure name="nbreVehicule_measure" class="java.lang.String">

<columnGroup name="nbreVehicule" height="66">
   <bucket class="java.lang.Integer">

Hence the bucketExpression does not need to be the same as the text displayed in the crosstabColumnHeader