• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Combine ArrayLists and sort

 
Ranch Hand
Posts: 455
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1873
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1873
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
oh Sorry!. its Ernest i wanted to write
regards
maulin
 
Jennifer Sohl
Ranch Hand
Posts: 455
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 455
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oops. Here it is!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic