Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sorting arraylist of object

Status
Not open for further replies.

slimmer

Programmer
Oct 21, 2002
11
US
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.


 
Hard to know for sure from your post but you might need to make your BondSummary class extend java.lang.Comparable

Hope this helps
-pete
 
Can you post your over-ridden compareTo method in your BondSummary class.

Will have a look,

Kev
 
Below is the compareTo method from my BondSummary class. I had to change the definition of my variable yearsToMature to Integer from int due to an error I was receiving.

public int compareTo(java.lang.Object obj) {
BondSummary bsummary = (BondSummary)obj;
return this.yearsToMature.compareTo(bsummary.yearsToMature);
 
If your yearsToMature is an int, use the following:

Code:
return this.yearsToMature - ((BondSummary)obj).yearsToMature;

If your yearsToMature is an integer, use the following:

Code:
return this.yearsToMature.compareTo(
  ((BondSummary)obj).yearsToMature);

Perhaps if you posted the ClassCastException stack trace, wrapped in [ code ] tags? Cheers, Neil
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top