PDA

View Full Version : Sorting



Anonymous
08-15-2002, 06:57 AM
I must have misunderstood something here. Do I have to sort my table prior to producing a report.

For example - If I am attempting to use an unsorted table and group them by a partiuclar column. The report enerated only groups items of the same name thar are in are continuos row wise

Do I have to implement some sort of sorting function in java to produce a sorted tablemodel that can then be used with JFreeReport.

Thnaks

Anonymous
08-15-2002, 07:39 AM
Hi James,

Yes, you need to provide the data in sorted order.

When I started out on JFreeReport I was going to make the sorting part of the report definition...but you can't be sure what lies behind the TableModel interface, and the sorting might end up being very inefficient, so I decided to leave it outside of JFreeReport, basically the responsibility of the developer supplying the data.

Perhaps this can be changed, I know it would be more convenient if JFreeReport took care of it. Suggestions are welcome...

Regards,

DG.

Anonymous
08-17-2002, 11:54 AM
I also faced exactly the same problem and solved this problem in the following way...

I created a class called ColumnComparator

-----------------------------------------------------------------------------------------------
import java.util.*;

public class ColumnComparator implements Comparator
{
protected int indices[];
protected boolean orders[];

public ColumnComparator(int index, boolean ascending)
{
this(new int[]{index}, new boolean[]{ascending});
}

public ColumnComparator(int[] indices, boolean[] orders)
{
this.indices = indices;
this.orders = orders;
}

public int compare(Object one, Object two) {
if (one instanceof Vector && two instanceof Vector) {
Vector vOne = (Vector)one;
Vector vTwo = (Vector)two;
for (int i = 0; i < indices.length; i++) {
Object oOne = vOne.elementAt(indices[i]);
Object oTwo = vTwo.elementAt(indices[i]);
if (oOne instanceof Comparable && oTwo instanceof Comparable) {
Comparable cOne = (Comparable)oOne;
Comparable cTwo = (Comparable)oTwo;
if (orders[i]) {
int result = cOne.compareTo(cTwo);
if (result == 0)
continue;
return result;
}
else {
int result = cTwo.compareTo(cOne);
if (result == 0)
continue;
return result;
}
}
}
}
return 1;
}
}
------------------------------------------------------------------------------------------------


In the tablemodel, I added the following code

/**
* Sorts a column in the specified order
*
* @param col the column to sort
* @param ascending the order to sort in
*/
public void sortColumn(int col, boolean ascending)
{
Collections.sort(rows, new ColumnComparator(col, ascending));
/* rows is the vector of vectors i.e all rows are stored in this vector*/
this.fireTableDataChanged();
}

------------------------------------------------------------------------------------------------

Now it's very easy for me sort any column I want in any order (ASC/DESC).

By the way, the sorting algorithm is fast enough, I tested this code on 1000 rows, it took only 50-70 millisecond to sort the whole column.




Hope this helps
Ash
------------------------------------------------------------------------------------------------

Anonymous
09-14-2002, 06:35 AM
Hi,

I tried ur solution, but i dont know why it's not working properly....

cud u give me the exact code.....

Anonymous
09-14-2002, 10:03 AM
Hi,

this solution does only work when using a DefaultTableModel (or a similar organized TableModel), which is internally organized as Vector of Vectors. Other ResultSets with a different internal ordering are not sortable this way (the ResultSetTableModel supplied in JFreeReport for instance).

This sorter depends on the existence of an rowVector (in the DefaultTableModel this vector is called "dataVector", so change the sortColumns function to:

/**
* Sorts a column in the specified order
*
* @param col the column to sort
* @param ascending the order to sort in
*/
public void sortColumn(int col, boolean ascending)
{
Collections.sort(dataVector, new ColumnComparator(col, ascending));
/* rows is the vector of vectors i.e all rows are stored in this vector*/
this.fireTableDataChanged();
}

and it will work for DefaultTableModels.

But this is no generally usable approach, as other systems may implement TableModels in a more efficent way as by storing all raw column values into a Vector of Vectors.

With a Database based approach as in the ResultSetTableModel (when using Scrollable-ResultSets), you are able to report over giantic datasets (think of a query with some 10.000 cusotmers and their ordered articles), the DataBase will do the memory management, rows that are not accessed are removed from memory and reloaded if needed. The memory footprint of such a query is rather small as the data is held on the database server and is only transfered on demand.

This approach is not limited to sql-databases, when using an advanced OR-Mapper you can do the same thing for Objects (JDO, Entity-Beans etc).

Have more fun,
said Thomas

Anonymous
09-16-2002, 01:59 AM
hi,

well, since i'm a new Java programmer......

u may appreciate the fact that i didnt get what's this 'datavector'....

Anonymous
09-16-2002, 06:44 AM
Hi Thomas,
Yes you are absolutely right becuase my approch of sorting does only work when using a DefaultTableModel.
This is what I had to as a means of last resort. I had no other way to sort the rows returned by the query. Becuase my database server was Lotus Domino server and I was using Lotus Notes JDBC driver to collect data. This driver happended to be a nightmare for me because It did not even allow me to restrict the rows returned (i.e Where Clause in SQL statement) let alone sorting.
As a result I was forced to adopt this solution.

Anonymous
02-04-2004, 06:36 AM
How to sort data in tablemodel from JFreeReport, I'm newbi and so much less experience in java. Any clue is very appreciated.
Actually the grouping is not working properly in JFreereport, maybe because of my data is not well sort. Sorry for my bad English

Taqua
02-04-2004, 01:21 PM
Hi,

yes, grouping needs the data ordered by the groups. If your data is read from a database, then use the SQL "order by" clause of the select statement to get a sorted resultset.

If you build your tablemodel yourself, then make sure that the backend collection of the tablemodel is sorted.

Without more detailed knowledge how your tablemodel is implemented, it is impossible to give a better advice.

Btw.: Sorting the tablemodel afterwards is certainly a bad idea, as this will eat more resources than doing the job right the first time.

Have more fun,
said Thomas