Interview Question – Sorting Time Strings


This is a pretty famous problem asked in most of the interviews, “Sorting the dates”. This is not a complicated one but sometimes, it becomes real confusing or starts giving unexpected results, if not properly taken care of. To learn more about similar problems, please visit the Interview Section.
Do not forget to use the Subscribe feature on the right to get updates on new articles.

Problem Statement- Sorting Time Strings

You are given an array of Time objects in which the times are in the following format “13:24:56.123” i.e. “HH:mm:SS.Z” and you need to sort them in ascending or descending order. This can be done very easily if we have to sort the dates because the java.util.Date class implements the Comparable interface, but there is nothing for time so we need to write our code.

Solution 1

There are multiple ways of solving this, I would write down the few of them. If you are thinking about comparing the hour component first, then the minutes, then the seconds and then the miliseconds, then you are right this can be used as one of the approaches. The only compulsion is that you need to separate each of the components from the String.

For e.g. below class can be created and used for this purpose:

After this we can either implement the Comparable interface and give an implementation to the compareTo() method or we can write our Custom Comparator by implementing the Comparator interface and provide an implementation to the compare() method.

To understand the Comparable and Comparator in detail read the article Comparable and Comparator – a detailed discussion

Two things still left in this approach, first, how to convert the String into our Time class and second, the implementation of the compare of compareTo() method.

Conversion is very easy, we can tokenize the String based on the delimiters ‘:’ and ‘.’ and we get an array of four Strings create an object out of it. Below code shows how to do it.

Now we need to write a comparator to compare the two Time objects, the below code is for the comparator:

Now that our infrastructure is ready, it is simple to sort now. We just need to add all the Time objects into the array and use the Collections.sort method as below:

Solution 2

THe above one was an object oriented and lengthy way of sorting times given as string. This should be only used when you are not sure if all the time strings will have the exact same format and two digits components. Here I show u another way of doing the same. Just put all the Strings in an array or list and use the sort method of Collections or Arrays class. By default the natural ordering of String for numbers is 0 < 1 < 2 < … < 9.

Below code demonstrates the same:

The only thing you need to take care is that all the times are in same format and they have the components in two digits.


Here we learnt a solution to a very simple problem of sorting time strings. You can employ the same technique to solve the sorting of time intervals.

Hope this helps, happy reading.