Report Sort By




I wonder if anyone knows how to use order by in jasper reports. Our old system (Crystal Reports) used a drop down list of order by fields, pulled from DB, and then this would be used in the report application viewer to sort by. I am unsure how to do the same thing in iReports. In CR there is a formula field menu and a sort by option where a case statement would be placed such as :

SELECT (@SortField)
CASE 'Name' : {Report.Name}
CASE 'City' : {Report.Town}
Default : {Report.State}

Our new system can support a similar DB set up with a table with a list of values to sort by and dropdown functionality built-in to the application. How do I achieve a similar sort of thing in iReport as i've described above or is there another way of doing it?



irnewbie's picture
Joined: Feb 1 2011 - 5:25am
Last seen: 8 years 4 months ago

2 Answers:



The <sortField/> element handles data sorting in JR.
You can set a <sortField/> in iReport opening the Report Query dialog, and clicking on the 'Sort Options...' button there.

Hope this helps,

shertage's picture
Joined: Sep 26 2006 - 8:06pm
Last seen: 1 month 2 weeks ago

Thanks for your help. I am not sure if this will do what we need it to do; we need to be able to pass a parameter value that the user picks from a drop down list in the application then use this in the report to sort. I am thinking along the lines of maybe a parameter in the query that this value the user picks is then sent to.

It would only sort by one field at a time.


irnewbie - 10 years 2 weeks ago


The builtin parameter SORT_FIELDS could be used to handle dynamic data sorting. This parameter should contain a list of dynamic JRSortField elements, based on user's input at runtime.
At the moment, there's no support in iReport to set this multivalue parameter, it only can be hardcoded at report filling time. This issue is already taken into consideration and will be solved as soon as possible. Until then, the below piece of code could be useful.

One should be careful with sortfields, because they are used only for in-memory sorting, ie data sorting is performed after retrieving unsorted data from the DB, and this could be both time and memory consuming. To perform optimized dynamic data sorting, is recommended to parametrize the query string instead, using the $P!{} syntax. To find out how to do it, please consult the query JR sample.


Map parameters = new HashMap();
List<JRSortField> sortList = new ArrayList<JRSortField>();
JRDesignSortField sortField = new JRDesignSortField();
//add other sortfields here
parameters.put(JRParameter.SORT_FIELDS, sortList);
JasperFillManager.fillReportToFile(<the_jasperReport>, parameters, <the_data_source>);</td></tr></tbody></table>
shertage's picture
Joined: Sep 26 2006 - 8:06pm
Last seen: 1 month 2 weeks ago

Thank you fo the reply and information.

irnewbie - 10 years 1 week ago