I have retrieved a resultset from the database in order of id number and years to mature, which is a calculated field. As I go through this resultset, I summarize data based on the order retrieved and add each unique element to an object class.
Prior to printing the data, I want to now sort the file so that it is in order of the calculated field then the id number. The object definition has the calculated field first and the id next.
I am not sure how to do this. I tried to use arrays.sort(object) or collections.sort(object) but received a ClassCastException. Looking through google, I see examples of comparable or comparator and TreeSet but I am not having success at making this work.
I want to sort the object back in the same name if possible, prior to printing the info in the jsp.
/*******************************************************/
CODE
public BondSummary[] calcBondSummary (BondHoldings[] bonds)
{
ArrayList bondSummary = new ArrayList();
Money totalBondValue = new Money(0);
String investOfficer = null;
String investOfficerName = null;
InvestmentOfficer io = null;
int yearsToMature = 0;
this.getBondSummaryData();
investOfficer = bonds[0].getInvestOfficerID();
investOfficerName = bonds[0].getInvestOfficerName();
yearsToMature = bonds[0].getYearsToMature();
String allBondTotal = null;
for (int index = 0; index < bonds.length - 1; index ++)
{
if (investOfficerName.equals(bonds[index].getInvestOfficerName()) && yearsToMature == bonds[index].getYearsToMature())
{
totalBondValue = totalBondValue.add(new Money(bonds[index].getMarketValue()));
}
else
{
allBondTotal = this.searchForBondValueByIO(investOfficer);
bondSummary.add(new BondSummary(yearsToMature,investOfficer, investOfficerName,totalBondValue,new Money(allBondTotal)));
totalBondValue = new Money(0);
investOfficer = bonds[index].getInvestOfficerID();
investOfficerName = bonds[index].getInvestOfficerName();
yearsToMature = bonds[index].getYearsToMature();
totalBondValue = totalBondValue.add(new Money(bonds[index].getMarketValue()));
}
}
************************************************************
PROBLEM HERE
// SortedSet sortedBonds = new TreeSet(bondSummary);
//Collections.sort(bondSummary);
return (BondSummary[])bondSummary.toArray(new BondSummary[0]);
END CODE
/*********************************************************/
The array of the object I want to return is what I want to sort but I am not sure why the results remains in order of what was retrieved from the database.
Any assistance and direction is greatly appreciated.
Prior to printing the data, I want to now sort the file so that it is in order of the calculated field then the id number. The object definition has the calculated field first and the id next.
I am not sure how to do this. I tried to use arrays.sort(object) or collections.sort(object) but received a ClassCastException. Looking through google, I see examples of comparable or comparator and TreeSet but I am not having success at making this work.
I want to sort the object back in the same name if possible, prior to printing the info in the jsp.
/*******************************************************/
CODE
public BondSummary[] calcBondSummary (BondHoldings[] bonds)
{
ArrayList bondSummary = new ArrayList();
Money totalBondValue = new Money(0);
String investOfficer = null;
String investOfficerName = null;
InvestmentOfficer io = null;
int yearsToMature = 0;
this.getBondSummaryData();
investOfficer = bonds[0].getInvestOfficerID();
investOfficerName = bonds[0].getInvestOfficerName();
yearsToMature = bonds[0].getYearsToMature();
String allBondTotal = null;
for (int index = 0; index < bonds.length - 1; index ++)
{
if (investOfficerName.equals(bonds[index].getInvestOfficerName()) && yearsToMature == bonds[index].getYearsToMature())
{
totalBondValue = totalBondValue.add(new Money(bonds[index].getMarketValue()));
}
else
{
allBondTotal = this.searchForBondValueByIO(investOfficer);
bondSummary.add(new BondSummary(yearsToMature,investOfficer, investOfficerName,totalBondValue,new Money(allBondTotal)));
totalBondValue = new Money(0);
investOfficer = bonds[index].getInvestOfficerID();
investOfficerName = bonds[index].getInvestOfficerName();
yearsToMature = bonds[index].getYearsToMature();
totalBondValue = totalBondValue.add(new Money(bonds[index].getMarketValue()));
}
}
************************************************************
PROBLEM HERE
// SortedSet sortedBonds = new TreeSet(bondSummary);
//Collections.sort(bondSummary);
return (BondSummary[])bondSummary.toArray(new BondSummary[0]);
END CODE
/*********************************************************/
The array of the object I want to return is what I want to sort but I am not sure why the results remains in order of what was retrieved from the database.
Any assistance and direction is greatly appreciated.