Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sorting elements within an array

Status
Not open for further replies.

daveh

Technical User
Jun 22, 2000
57
GB
Hi, I'm working on a small piece of software and I've got to a little problem. The software has a Train class, which has two integers (departureTime and arrivalTime). In my main controlling class, there are methods to add Trains to a Trains array (called "trains"), to delete these trains, to display the trains present in the form of a timetable, etc.

I am trying to get it so that the trains, when the user decides to display the timetable, are display in ascending order based on their departureTime. I can't use a simply Array.sort() as the array is an array of Train objects, and not of the integers themselves.

I've played round with FOR loops, etc. I have a Time class, which has in it a method that goes as follows...

public int compare(Time t) {
// compare this Time with Time t
if (this.minutes == t.minutes) return 0;
if (this.minutes > t.minutes) return 1;
return -1;
}

So I can easily find out whether the individual train is sooner or later than the other, but it is a case of doing this in such a way that the trains end up in an order in the array of ascending numerical order by the departureTime.

A seperate array could be created, "displayTrains" which holds them in, that would be fine. I only need the trains to be sorted by departuretime for the display method, they can be in any order in the rest of the program.

Any ideas? The following is what I have so far for the display method, though where the sorting code will go there is a comment....

//method to display the trains currently in the system, in departure time order
public static void displayTrains() {
String trainsDisplay="", sdeptTime, sarrTime, sjourTime;
Time deptTime = new Time();
Time arrTime = new Time();
Time jourTime = new Time();
Time sortDeptTimeA = new Time();
Time sortDeptTimeB = new Time();
Train [] displayTrains = new Train[numberTrains];
int counter1=0, counter2=0, counter=0, compare, arrayPos=0;
boolean earlier=false;

//code to sort here

trainsDisplay = "Loughborough to Leicester Train Timetable:\n\n";
for (counter=0; counter<numberTrains; counter++) {
deptTime = displayTrains[counter].getDepartureTime();
arrTime = displayTrains[counter].getArrivalTime();
jourTime = displayTrains[counter].getJourneyTime();
sdeptTime = deptTime.toString();
sarrTime = arrTime.toString();
sjourTime = jourTime.toString();
trainsDisplay = trainsDisplay + &quot;Departure: &quot; + sdeptTime +
&quot; Arrival: &quot; + sarrTime + &quot; Duration: &quot; + sjourTime + &quot;\n&quot;;
}
JOptionPane.showMessageDialog(null, trainsDisplay);
}

I would really appreciate any ideas people have, I'm learning Java at the moment, so if its really simple, then the code would also be greatly appreciated!

Kindest regards,
David.
 
David,

Have you looked at the java.lang.Comparable interface? It should provide you with a solution.

-pete
I just can't seem to get back my IntelliSense
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top