jQuery in Action, 2nd edition*
The moose likes Java in General and the fly likes Combine ArrayLists and sort Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Combine ArrayLists and sort " Watch "Combine ArrayLists and sort " New topic
Author

Combine ArrayLists and sort

Jennifer Sohl
Ranch Hand

Joined: Feb 28, 2001
Posts: 455
Hi. I have a couple of classes I have created that contain getter and setter methods for the variables I have defined in them.
These classes are called INBlankData and INAllocData.
In another class called INVData, I have defined the above two classes as ArrayList variables, which have methods that set the elements and return the elements.
So I have an ArrayList of INBlankData objects and INAllocData objects.
Now, I want to take these two ArrayLists and combine them into one. Then I want to be able to sort this new ArrayList which would be a
combination of the INBlankData objects and the INAllocData objects.
Both of the INBlankData and INAllocData objects have a common variable in them which is a date.
Is there any way I can sort the data in the new ArrayList by this variable??
Or is there a better way of doing this? Before, I tried getting all of the data combined together by doing it in a SQL statement, but it became too complex, I couldn't even think of how to do it in SQL.
Sorry if this is confusing. I'm pretty stumped, so it's hard for me to explain.
Thanks for any help!
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

This is not too hard to do.
1) Call arrayList1.addAll(arrayList2) to combine the lists.
2) Implement a java.util.Comparator object that can properly compare the two types of object. The comparator may have to use "instanceof" to figure out what kind of object each of the arguments to compare() is.
3) Then use the static method java.util.Collections.sort(List, Comparator) to sort the combined ArrayList.


[Jess in Action][AskingGoodQuestions]
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1871
hi Jennifer
i guess i understand ur problem.
from ur requirement it seems INBlankData and INAllocData objects are related via 'date' right? meaning they have this 'must' common variable.
u can do following probably,
- create an abstract class INData and put the date field in there and get/set methods for it etc in there. remember u dont have to create any methods which are abstract if u want. just declare the class to be abstract.
- make INData implementing Comparable OR make it extend Comparator and implement appropriate compare() method based on date field
- inherit INBlankData and INAllocData from that class and u r done i guess
regards
maulin
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1871
or yes as Eric suggested u can use Comparator with two object types and then do some little magic and all...
but if u really see common behavior (as from name of IN*Data u have) then its better to make some abstraction or interface and work from there...
regards
maulin
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1871
oh Sorry!. its Ernest i wanted to write
regards
maulin
Jennifer Sohl
Ranch Hand

Joined: Feb 28, 2001
Posts: 455
Hi! Thanks for the replies. I have my data all in one class now
called StockAvailable.
However I am a bit confused on how the sorting works. This is a class that
implements Comparator , but I didn't get very far before I found out I have no clue how to write a comparator class. I've looked at various examples, but I don't understand them.

Now what do I do? How do I pull the date field out of this class?
What am I comparing it to?
Also, can I sort on a date in StockAvail, and then have a secondary sort on an order number from StockAvail?
Any help would be appreciated.
Thanks again!
[ August 14, 2003: Message edited by: Jennifer Sohl ]
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

We don't know how to get the Date field out of your StockAvailable class, because we haven't seen it!
Jennifer Sohl
Ranch Hand

Joined: Feb 28, 2001
Posts: 455
Oops. Here it is!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Combine ArrayLists and sort
 
Similar Threads
JDBC Interfaces
Sun Cirtification
ArrayList
Sorting two Classes
Sorting two ArrayLists, one dependent on the other