Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Searching inside a collection of user objects

 
H Ritwick
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi folks,
Pls suggest a good way of doing the below requirement.
I have a collection of userprofile objects (around 500..not in order).
I need to display these on a jsp(easy part)..which has filter keys like
a-h,I-P,Q-z like filters on the page. On click of any of the links I need to display the user names starting with I thro P in asc order.
I am thinking of using comparator for this.
compare(obj1,obj2)
Profile p1 = (Profile)obj1;
Profile p2 = (Profile)obj1;
then take the substring of user name's first char from p1 and p2
then if c1 > 'A' and c1 < 'H' return 1
else if c1 < 'A' and c1 > 'H' return -1
else if equal return 0

Is this a good way of doing this..Please suggest.
Thanks
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would either :
1. Go through all user and extract those whose name begins with I-P
2. Sort the new list
Or
1. Manage all users in a sortable list (e.g. a SortedSet)
2. Extract users whose name begins with I-P

The compare() should return something like :
return p1.getName().compareTo(p2.getName())
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic