How to Sort an array of different objects.

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Sep 2008
Posts: 11
Reputation: Kamal_Java is an unknown quantity at this point 
Solved Threads: 0
Kamal_Java Kamal_Java is offline Offline
Newbie Poster
 
0
  #1
Sep 28th, 2008
Hi ... [ Need ur suggestion NOT code .... ]

This is one part of my assignment .. I need your help on how to do this . Below is my short description of my problem ...

I have two classes Named Passenger class and container class .

Passenger class has its own variables like passenger name ( String ) , ID ( int ), and Injury Level (An enum class which has priority like CRITICAL ,MODERATE,SLIGHT,NONE ).

Employee class has its own variable like containerID ( int ) , and ContainerType ( this can be one of the three types URGENT , IMPORTANT , or GENERAL - these are from enum class )

I will be taking this passenger details[ has 100 of passenger details ] and container details [ 100 of container ] from a file and write to a single Array . I used Object array Object[] array as advised in this form to store different objects [Passenger and Conatiner] . Now i want to sort this array based on criteria as mentioned below.

1. Sort People , most Highest priority first .
- This must sort the array in such a way that highest priority first ex:CRITICAL followed by MODERATE and so on ... Container are placed after sorted passengers.

2. Sort Containers ,most urgent first.
- This must sort the URGENT Container first followed by IMPORTANT and so on ... Any passenger are placed after the sorted container.


I have an interface function compareTo() in my Passenger and container class to implement . How do i sort so . Thanks.

Hi ... [ Need ur suggestion NOT code .... ]

This is one part of my assignment .. I need your help on how to do this . Below is my short description of my problem ...

I have two classes Named Passenger class and container class .

Passenger class has its own variables like passenger name ( String ) , ID ( int ), and Injury Level (An enum class which has priority like CRITICAL ,MODERATE,SLIGHT,NONE ).

Employee class has its own variable like containerID ( int ) , and ContainerType ( this can be one of the three types URGENT , IMPORTANT , or GENERAL - these are from enum class )

I will be taking this passenger details[ has 100 of passenger details ] and container details [ 100 of container ] from a file and write to a single Array . I used Object array Object[] array as advised in this form to store different objects [Passenger and Conatiner] . Now i want to sort this array based on criteria as mentioned below.

1. Sort People , most Highest priority first .
- This must sort the array in such a way that highest priority first ex:CRITICAL followed by MODERATE and so on ... Container are placed after sorted passengers.

2. Sort Containers ,most urgent first.
- This must sort the URGENT Container first followed by IMPORTANT and so on ... Any passenger are placed after the sorted container.


I have an interface function compareTo() in my Passenger and container class to implement . How do i sort so . Thanks.
Last edited by Ancient Dragon; Sep 29th, 2008 at 6:04 am.
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 706
Reputation: stultuske is a jewel in the rough stultuske is a jewel in the rough stultuske is a jewel in the rough 
Solved Threads: 84
stultuske's Avatar
stultuske stultuske is offline Offline
Master Poster

Re: How to Sort an array of different objects.

 
0
  #2
Sep 29th, 2008
by comparing the values on which you need to sort and implementing that comparisation in (for instance) a bubble-sort method
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 11
Reputation: Kamal_Java is an unknown quantity at this point 
Solved Threads: 0
Kamal_Java Kamal_Java is offline Offline
Newbie Poster

Re: How to Sort an array of different objects.

 
0
  #3
Sep 29th, 2008
how do i do bubble sort when i have two different objects in that array..
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 3
Reputation: davidjonas is an unknown quantity at this point 
Solved Threads: 0
davidjonas davidjonas is offline Offline
Newbie Poster

Re: How to Sort an array of different objects.

 
0
  #4
Sep 29th, 2008
Hi!

I am not sure specifically what your problem is, if it is the sorting it self or the fact that you have an array of different childs of Object.

But anyway, and although I am not sure if these are the best solutions performancewise, I see 3 options for you:

1. when reading the files, save the data to two different temporary arrays, order them, and merge them to a Object array in the end.


2. If you have the array with everything in it, you can run through it with a for statement making something like:

  1. Object [] passengersTemp = new Passenger[nPassengers];
  2. Object [] containersTemp = new Container[nContainers];
  3.  
  4. for(int i = 0; i < myObjectArray.lenght; i++)
  5. {
  6. if(myObjectArray[i] instanceof Passenger)
  7. {
  8. //sort and insert in passengersTemp
  9. }
  10. else if(myObjectArray[i] instanceof Container)
  11. {
  12. //sort and insert in containersTemp
  13. }
  14. }
  15. //empty myObjectArray
  16. //insert all contents of passengersTemp into myObjectArray
  17. //insert all contents of containersTemp into myObjectArray

3. have only one temporary array and move all the passengers ordered to the first 100 (0 - 99) positions and then move all the containers to the second 100 positions (100 - 199).


if your problem is the sorting algorithm check out these examples of the most comon algorithms: http://www.cs.ubc.ca/~harrison/Java/sorting-demo.html
Last edited by davidjonas; Sep 29th, 2008 at 9:24 am.
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 706
Reputation: stultuske is a jewel in the rough stultuske is a jewel in the rough stultuske is a jewel in the rough 
Solved Threads: 84
stultuske's Avatar
stultuske stultuske is offline Offline
Master Poster

Re: How to Sort an array of different objects.

 
0
  #5
Sep 29th, 2008
Originally Posted by Kamal_Java View Post
how do i do bubble sort when i have two different objects in that array..
what would the use otherwise be? sorting a single Object seems kinda boring, wouldn't you agree? now, what I think you mean, is that you need to sort several Objects which are instances from different classes.

what I believe to make out of your assignment, is that you have to sort the passengers (which aren't different kind of Objects) based on their Injury Level (as in: all Critical cases first, then the moderate ones, and last (and yes, in this case least (important) the slightly injured once)

once you've done this, you put the passengers that are critical in a container that is urgent
those that are moderate, in important and the others in general
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC