| | |
How to Sort an array of different objects.
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Sep 2008
Posts: 11
Reputation:
Solved Threads: 0
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.
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.
•
•
Join Date: Sep 2008
Posts: 3
Reputation:
Solved Threads: 0
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:
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
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:
Java Syntax (Toggle Plain Text)
Object [] passengersTemp = new Passenger[nPassengers]; Object [] containersTemp = new Container[nContainers]; for(int i = 0; i < myObjectArray.lenght; i++) { if(myObjectArray[i] instanceof Passenger) { //sort and insert in passengersTemp } else if(myObjectArray[i] instanceof Container) { //sort and insert in containersTemp } } //empty myObjectArray //insert all contents of passengersTemp into myObjectArray //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.
•
•
•
•
how do i do bubble sort when i have two different objects in that array..
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
![]() |
Similar Threads
- Sorting on Class members (C++)
- finiding min in array till satisfies condition explained in prbm stmnt (C++)
- adding & subtracting objects from a list (C++)
- Source Code that don't work? (Java)
- Sorting from a file (C)
- Lab..Pls Help (Java)
- Improving exercise? (C)
- Array troubles? (C++)
- help with sort using Calendar class getting null pointer exception (Java)
- AIRPLANE PROGRAM (C++)
Other Threads in the Java Forum
- Previous Thread: How can i select best ftp mirror from a list
- Next Thread: UDP packet
| Thread Tools | Search this Thread |
Tag cloud for Java
addressbook android api apple applet application arguments array arrays automation awt binary bluetooth button calculator chat class classes client code columns component converter database draw eclipse error errors event exception file fractal ftp game givemetehcodez graphics gridlayout gui helpwithhomework html ide image inetaddress input integer invokingapacheantprogrammatically j2me java javaprojects jme jmf jni jpanel julia link linux list loop map method methods midlethttpconnection mobile netbeans newbie number object objects openjavafx oracle php print problem program programming project projects recursion rim scanner screen server set signing size smart sms socket sort sql storm string support swing test threads time tree webservices windows





