1
new Flight("Philadelphia", "Las Vegas", "Southwest", new GregorianCalendar(2007, 1, 12, 5, 0, 0),new GregorianCalendar(2007, 1, 12, 10, 0, 0))


new Flight("Trenton", "Blacksburg", "Trans-National Air", new GregorianCalendar(2007, 7, 11, 13, 30, 0), new GregorianCalendar(2007, 8, 11, 14, 30, 0)),

This is my Flight Object 1 & 2, there are many more inside an object array called flight. Recently I sorted the list be source city and departure time.

Now I'm trying to figure out how to sort an array of objects, by departure & arrival time.

This is all using the comparison method.

Could someone show me some code, sorting the two objects by departure & arrival time, or get me started I'm kinda stumped here.

Chris
  • 11
  • 1
  • Take a look at [Arrays.sort()](http://docs.oracle.com/javase/8/docs/api/java/util/Arrays.html#sort-T:A-java.util.Comparator-). You should always look into jdk first before trying to write a utility method. Plenty of them are already there. – glee8e Feb 25 '16 at 05:26
  • accept the answer please if that solved your problem thank you – Faraz Feb 26 '16 at 07:38

3 Answers3

0

You can use Collections.sort method

Collections.sort(flight, Flight.getDepartureTime()); //First argument is name of the list

and

Collections.sort(flight, Flight.getArrivalTime());

Or you can use Arrays.sort method like this:

Arrays.sort(flight, Flight.getDepartureTime()); //First argument is name of the array

and

Arrays.sort(flight, Flight.getArrivalTime()); 
Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252
Faraz
  • 6,025
  • 5
  • 31
  • 88
0

You could implement Comparable interface in your Flight class, then override the compareTo() method after that you can do comparing inside.

Bahramdun Adil
  • 5,907
  • 7
  • 35
  • 68
0

If I understand your question correctly, you can accomplish this using Collections.sort, and by defining a compareTo method for your object, as seen in this answer: How to sort by two fields in Java?

Community
  • 1
  • 1
nman
  • 57
  • 1
  • 8