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.

tejaswi438 commented: I have the same problem with my assignment,can you please help me to know, how to sort an array of objects based on alphabetical order +0

Recommended Answers

All 4 Replies

by comparing the values on which you need to sort and implementing that comparisation in (for instance) a bubble-sort method

how do i do bubble sort when i have two different objects in that array..

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:

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

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.