TimeSorter: 12 hour clock

953 Views Asked by At

I want to sort these times in order:

4:05 AM
5:04 PM
6:04 AM
4:05 PM
5:04 AM
12:01 AM
12:01 PM

using Time class with

public int compareTo(Time t)

method.

if(this.getMeridians() != t.getMeridians())
   return this.getMeridians().compareTo(t.getMeridians());

to sorted AM and PM, but I don't know how to sort hours and minutes. It's in 12-hour clock form, so 12:01 AM should be very first on the list. In order to do that, how should I fill up the compareTo(Time t)?

It should be like this.

12:01 AM
4:05 AM
5:04 AM
6:04 AM
12:01 PM    
4:05 PM
5:04 PM
2

There are 2 best solutions below

0
On

The answer by Guy is correct.

For fun I did the same kind of code but using the Joda-Time 2.3 library.

My code assumes you truly want times only, without dates. Therefore, you'll not get handling of Daylight Saving Time or other issues.

// © 2013 Basil Bourque. This source code may be used freely forever by anyone taking full responsibility for doing so.
// import org.joda.time.*;
// import org.joda.time.format.*;

DateTimeFormatter formatter = DateTimeFormat.forPattern( "hh:mm a" );
// LocalTime time = formatter.parseLocalTime( "4:05 PM" );

// Create a list of org.joda.time.LocalTime instances, created by parsing strings.
List list = new ArrayList( 7 );
list.add( formatter.parseLocalTime( "4:05 AM" ) );
list.add( formatter.parseLocalTime( "5:04 PM" ) );
list.add( formatter.parseLocalTime( "6:04 AM" ) );
list.add( formatter.parseLocalTime( "4:05 PM" ) );
list.add( formatter.parseLocalTime( "5:04 AM" ) );
list.add( formatter.parseLocalTime( "12:01 AM" ) );
list.add( formatter.parseLocalTime( "12:01 PM" ) );
System.out.println( "Unsorted: " + Arrays.toString( list.toArray() ) );

// Sort that list.
Collections.sort( list );
System.out.println( "Sorted: " + Arrays.toString( list.toArray() ) );

When run…

Unsorted: [04:05:00.000, 17:04:00.000, 06:04:00.000, 16:05:00.000, 05:04:00.000, 00:01:00.000, 12:01:00.000]
Sorted: [00:01:00.000, 04:05:00.000, 05:04:00.000, 06:04:00.000, 12:01:00.000, 16:05:00.000, 17:04:00.000]

If you need the values output again as AM/PM format, search StackOverflow.com for examples of using formatters in Joda-Time to create strings ("print" method).

0
On
  1. parse those strings to Date objects
  2. put them in a collection
  3. use collection.sort() to sort the collection

    import java.text.DateFormat;
    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.Date;
    import java.util.List;
    import java.util.Locale;
    
    String[] sArr = new String[] { "4:05 AM", "5:04 PM", "6:04 AM",
            "4:05 PM", "5:04 AM", "12:01 AM", "12:01 PM"};
    
    DateFormat dateFormat = new SimpleDateFormat("hh:mm a", Locale.US);
    List<Date> dates = new ArrayList<Date>();
    try {
        for (String s : sArr) {
            Date date = dateFormat.parse(s);
            dates.add(date);
        }
    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    
    Collections.sort(dates);
    
    for (Date date : dates) {
        System.out.println(dateFormat.format(date));
    }