Sort list<object> taking date where the date is of type string

I have a list of object where the data is as follows:

public class User {
    public String name;
    public String date;
    public String getName() {
        return name;
    public void setName(String name) { = name;
    public String getDate() {
        return date;
    public void setDate(String date) { = date;
  • Xyz 11-10-2012
  • Abc 11-10-2012
  • Pqr 20-04-2012
  • Ijk 24-08-2012
  • Mno 22-05-2014

How do i sort the data i.e names according to the dates which comes as String. I need it to be sorted by year then month and then date. I have tried using the below code but

List<User> list = The list where i get my data
List<User> listTemp =

The Output comes as

  • 8/24/2012

  • 5/22/14

  • 4/20/12
  • 10/11/12
  • 10/11/12

P.s - I cant change the type of String to Date.

7 answers

  • answered 2018-11-08 08:03 PumpkinBreath

    You could implement the Comparable interface in your User class and then override the compareTo() method so that you are sorting the dates in whatever order you need. The compareTo() returns an int value, either -1 0 or 1

  • answered 2018-11-08 08:07 Mershel

    Try this method:

    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy");
    List<User> listTemp = list .stream()
                .sorted((u1, u2) -> LocalDate.parse(u1.getDate(), formatter).compareTo(LocalDate.parse(u2.getDate(), formatter)))

  • answered 2018-11-08 08:15 Dimitris

    The reason that your code does not work is because currently you are comparing strings. The string comparator is not meant to compare dates, I expect to sort alphabetically or something along this line.

    I am not sure why you say that you cannot parse the string to date and use the comparator of the date, it would make your life much easier. It may need some string formatting before you do that, but you will have to do string manipulation either way.

    If you definitely cannot parse the strings to dates you will need to extract the date parts of the string either by string manipulation or by regex. Then you will need to convert them to numbers. You then have to implement comparator in your class and use the numbers that you have generated to make the sorting work as you wish in the comparator.

    Overall I would try to find a way to convert the strings to dates, it seems like a much cleaner solution to me.

  • answered 2018-11-08 08:19 Schidu Luca

    Usually in this case it's better to convert String to a Date, but anyway here's my solution without converting:

    List<User> listTemp =
                .sorted(Comparator.comparing(u -> {
                    String[] s = u.getDate().split("-");
                    return s[2] + s[1] + s[0];

    P.S Obviously I do not recommend using this in real projects

  • answered 2018-11-08 08:20 JMD

    Please find the working code below:

    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy");
        User2 u1 = new User2("User1","08-11-2018");
        User2 u2 = new User2("User2","09-11-2018");
        List<User2> userList = new ArrayList<User2>();
        List<User2> listTemp = userList .stream()
                    .sorted((x, y) -> LocalDate.parse(x.getDate(), formatter).compareTo(LocalDate.parse(y.getDate(), formatter)))
        listTemp.forEach(user -> {

  • answered 2018-11-08 09:47 malvern dongeni

     List<User> user =

    The above code should help. I have used the following sample data

    Abc 03-24-2011

    Abc 03-19-2011

    Abc 03-21-2011

    This will give you a clear picture of how the sorting is done.

    Name -> Year ->Month -> Date

    For your information if streams are being used,modification done on the stream does not affect your original data source.Hence all the above solution changes only the stream not your User object.

  • answered 2018-11-08 12:02 ETO

    First of all you don't need the String-Date conversion here. What you need is a custom comparator able to deal with your custom format:

    This is an example of working comparator. Pay attention to Comparator.comapring and .thenComparing methods. They allow you to perform consecutive comparison.

    Assuming your format is mm/dd/yyyy the following snipped will do the trick. It will compare the yyyy part first, then mm and dd:

    final String delimiter = "/";
        .sorted((user1, user2) -> {
            String[] date1 = user1.getDate().split(delimiter);
            String[] date2 = user2.getDate().split(delimiter);
            return Comparator.comparing((String[] arr) -> arr[2])
                             .thenComparing(arr -> arr[0])
                             .thenComparing(arr -> arr[1])
                             .compare(date1, date2);

    A bit less verbose solution:

    Function<User, String[]> splitDate = u -> u.getDate().split("/");
        .sorted((u1, u2) -> Comparator.comparing((String[] arr) -> arr[2])
                                      .thenComparing(arr -> arr[0])
                                      .thenComparing(arr -> arr[1])
                                      .compare(splitDate.apply(u1), splitDate.apply(u2)))