Hi, I have a ArrayList containing the values retreived from the DB. I need to sort the data in two levels. The primary level is the field containing the Date and if the sorted values have the same dates the data needs to be sorted with the field containing Names. How do I do it? I have used the comparator to sort individually how do I club the results?
Thanks, Vinitha
Jeff Rummings
Ranch Hand
Joined: Dec 20, 2007
Posts: 42
posted
0
Either a Comparator or Comparable (whatever is more appropriate) will do the job. If you're using a Comparator, you could do something like this:
Vinitha Pai
Greenhorn
Joined: Nov 14, 2007
Posts: 6
posted
0
Thanks for the response. But I need to retain the result of the first sort and then sort upon it. The second sort criteria should not effect the first result if there is no similar data.
Petrus Pelser
Ranch Hand
Joined: Feb 20, 2006
Posts: 132
posted
0
Sort first according to date, then take the sorted list and sort that again using a simple conditional statement on the date and name. (while date is equal => sort name)
Joanne Neal
Rancher
Joined: Aug 05, 2005
Posts: 3011
9
posted
0
Originally posted by Vinitha Pai: Thanks for the response. But I need to retain the result of the first sort and then sort upon it. The second sort criteria should not effect the first result if there is no similar data.
Did you try Arman's suggestion ? I think you will find it does what you want.
Thank you all for your timely reply. Yes! It did work. But instead of compareTo I used compare() because this wasnt working when I needed to change the sortorder.
Cheers, Vinitha
Bill Shirley
Ranch Hand
Joined: Nov 08, 2007
Posts: 457
posted
0
You may also be able to have the database sort it for you before it is returned, depending on how your data is persisted.
Most tools overlie SQL and us it to fetch. If you have access to the SQL (EJB QL, etc.) you can have the database do the heavy lifting before it is retrieved.
This could be particularly useful if you are dealing with 10s of 1000s of records, or if you want to only return a subset of the records.
Bill Shirley - bshirley - frazerbilt.com
if (Posts < 30) you.read( JavaRanchFAQ);