• Post Reply Bookmark Topic Watch Topic
  • New Topic

Sorting my Value Objects - Problems - Help!  RSS feed

 
Christopher Frankland
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am trying to implement a sorting mechanism on the 'month' field of my value object. I am having some problems getting it working. Any advice would be much appreciated. Code:

Servlet.java:



CaseInsensitiveComparator.java:



When my servlet is invoked, I get the following:

com.dr.vo.MonthlyScoreVO@4201d080
com.dr.vo.MonthlyScoreVO@42dd5080
com.dr.vo.MonthlyScoreVO@43145080
com.dr.vo.MonthlyScoreVO@4338d080
com.dr.vo.MonthlyScoreVO@435d5080
com.dr.vo.MonthlyScoreVO@43efd080
com.dr.vo.MonthlyScoreVO@4481d080
com.dr.vo.MonthlyScoreVO@44a65080
com.dr.vo.MonthlyScoreVO@44cad080
com.dr.vo.MonthlyScoreVO@45c89080
com.dr.vo.MonthlyScoreVO@45f81080


Any ideas?


Thanks.
 
Dave Wingate
Ranch Hand
Posts: 262
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Unless the toString() method of your MonthlyScoreVO returns just the month value by which you wish to sort, you'll need to update your comparator.

Right now the comparator is sorting on the toString() value of the VO, not the 'month' field of the VO.

Instead of :
String s1 = o1.toString().toUpperCase();

Try:
String s1 = ((MonthlyScoreVO) o1).getMonth().toUpperCase();
 
Christopher Frankland
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Dave,

Thanks for the reply. I am still getting the same output, however.

Modified code:



Output:

com.dr.vo.MonthlyScoreVO@1bcd5155
com.dr.vo.MonthlyScoreVO@19879155
com.dr.vo.MonthlyScoreVO@1a9d5155
com.dr.vo.MonthlyScoreVO@1c12d155
com.dr.vo.MonthlyScoreVO@1b649155
com.dr.vo.MonthlyScoreVO@1ae7d155
com.dr.vo.MonthlyScoreVO@1a1e9155
com.dr.vo.MonthlyScoreVO@1c931155
com.dr.vo.MonthlyScoreVO@1bea5155
com.dr.vo.MonthlyScoreVO@19ac1155
com.dr.vo.MonthlyScoreVO@1da95155
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, you need a toString() implementation that allows you to see what's in those MonthlyScoreVO objects. They probably are being sorted correctly, but the toString() inherited from Object doesn't let you see that. So write a toString() method.
 
Christopher Frankland
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cheers Jim. That worked. I added a toString() method in my VO and it did the trick. Thanks for you feedback guys.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!