Jump to content
We've recently updated our Privacy Statement, available here ×
  • How to Implement Crosstab Column Group Custom Sorting


    asimkin
    • Features: Reports Version: v6.4 Product: JasperReports® Library

    Issue Description

    I created a JRXML report with Crosstab. One of group column based on a String field which returns

    period values like 'Aug 2017', 'Jan 2017' etc.

    By default, the column sorted alphabetically.

    How can I change it to sort the values as Date type, Jan-2017, Feb-2017 an so on?


    Resolution

    Solution requires creating a custom Java class that implements Comparator interface.

    That class converts strings with values like 'Apr 2017' into a Date value and then compare them.

    In order to use custom comparator in Crosstab, report hidden parameter required to call the custom class:

        <parameter name="myOrder" class="com.sample.DateComparator" isForPrompting="false">

            <defaultValueExpression><![CDATA[new DateComparator()]]></defaultValueExpression>

        </parameter>

    Then, the parameter is used in crosstab column group comparator expression:

                    <columnGroup name="period" height="20">

                        <bucket class="java.lang.String">

                            <bucketExpression><![CDATA[$F{period}]]></bucketExpression>

                            <comparatorExpression><![CDATA[$P{myOrder}]]></comparatorExpression>

                       </bucket>

            ............

            </columnGroup>

     

    The custom class should be specified in Import property of the report:

        <import value="com.sample.*"/>

    Attached are:

     - custom comparator Java code

     - compiled package

     - JRXML report


    Ref. Case 01466303

    datecomparator_0.java

    datecomparator.jar

    support_period_sortorder.jrxml


    User Feedback

    Recommended Comments

    There are no comments to display.



    Create an account or sign in to comment

    You need to be a member in order to leave a comment

    Create an account

    Sign up for a new account in our community. It's easy!

    Register a new account

    Sign in

    Already have an account? Sign in here.

    Sign In Now

×
×
  • Create New...