• Post Reply Bookmark Topic Watch Topic
  • New Topic

ResultSet sorting  RSS feed

 
shomps shimps
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have a resultset in a program which along with other cols has two cols called emailone and emailtwo(Strings).
I have to sort this resultset once on emailone and again on emailtwo. I have found out that if I take each row of the resultset into an object whose class implements comparable interface then I can sort such array of objects using Arrays.sort(object[]).
Now in my compareTo method to sort on emailone I can write something like :
public int compareTo(Object O){
return emailone.CompareTo((myObject)O.emailone);//eamilone is a string in myObject class
}
This will effectively serve my purpose of sorting the result set on emailone. But now I want to sort the resultset again on emailtwo in the same program. How to do this ? Do I have to create another class that implements comparable and then give a different definition for compareTo method.And then create an array of objects of this class and then use Arrays.sort(objects[]) to sort on emailtwo ?
Please help.
Thanks in advance.
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Both Arrays.sort() and Collections.sort() have overloaded versions that take a Comparator. Just create a Comparator for each sorting order that you want.
- Peter
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!