Reputation: 35
I need to sort the status reports by date. Before I'm calling the addItem method the sorting should be done or I have to compare with previous report by report date. It is to be stated that the getReportDate()[ which is of type JVDate] method can be used to get the report date of a status report. Would you please help to sort the status reports:
public void doImport( TRDataReader in )
throws IOException, TRException
{
in.start( getClassTag() ); // get the class tag
// import the set's flags from a datareader
importFlags( in );
beginLoad ();
final String restag = new TRStatusReport().getClassTag ();
while (in.nextToken (restag)) {
addItem (new TRStatusReport (in));
}
endLoad ();
in.end (getClassTag ());
}
Upvotes: 1
Views: 152
Reputation: 4182
Simply use Java's built-in sorting algorithm, by specifying the appropriate comparator. Something like to following:
public void doImport(TRDataReader in) throws IOException, TRException {
in.start(getClassTag()); // get the class tag
importFlags(in); // import the set's flags from a datareader
// Add the reports to a temporary list first.
final String restag = new TRStatusReport().getClassTag();
List<TRStatusReport> list = new ArrayList<TRStatusReport>();
while (in.nextToken(restag)) {
list.add(new TRStatusReport(in));
}
// Now sort them.
TRStatusReport[] array = list.toArray(new TRStatusReport[]{});
Collections.sort(array, new Comparator<TRStatusReport>() {
@Override
public int compare(TRStatusReport o1, TRStatusReport o2) {
return o1.getReportDate().compareTo(o2.getReportDate());
}
});
// Add it to the internal list.
beginLoad();
for (int i = 0; i < array.length; i++) {
addItem(array[i]);
}
endLoad();
in.end( getClassTag() );
}
You will have to find a way to compare the dates if they are not Java Date objects. I've written this code blindly (I don't know what the objects are) and with some assumptions. For example, the beginLoad() and endLoad() methods... are they for the list or for the reading? ...if so, they may need to be placed around the while clause where the objects are loaded and added to the temporary list.
Upvotes: 2