-1

I have a list like ArrayList<DateFrequency>

private static ArrayList<DateFrequency> getUnsortedDateFrequencyList() 
{
    ArrayList<DateFrequency> list = new ArrayList<>();
    list.add( new DateFrequency(05/10/2020, "60-DAYS") );
    list.add( new DateFrequency(05/10/2020, "30-DAYS") );
    list.add( new DateFrequency(05/11/2020, "30-DAYS") );
    list.add( new DateFrequency(05/12/2020, "60-DAYS") );
    list.add( new DateFrequency(05/11/2020, "90-DAYS") );
}

I need Sorted List as

05/10/2020, "30-DAYS"
05/10/2020, "60-DAYS"
05/11/2020, "30-DAYS"
05/11/2020, "90-DAYS"
05/12/2020, "60-DAYS"
Federico klez Culloca
  • 26,308
  • 17
  • 56
  • 95
  • 1
    Try to use comparator. – Tarun May 12 '20 at 09:11
  • The solution depends on what `DateFrequency` is. – Federico klez Culloca May 12 '20 at 09:16
  • It’s pretty basic stuff already explained in may places. Search before asking to get good answers faster than anyone can type a new answer here. [I downvoted because research must be done to ask a good question](http://idownvotedbecau.se/noresearch/). – Ole V.V. May 12 '20 at 15:45
  • Don’t keep your dates and frequencies as strings inside your object. Use `LocalDate` for the dates and either `int` or some enum type for the frequencies; you may also consider `Period`. You can always format the data in a user-friendly way when you need to present it. – Ole V.V. May 12 '20 at 15:48

2 Answers2

0

You can use java 8 stream to achieve the same as shown below.

List<String> sortedList = list.stream().sorted().collect(Collectors.toList());
Federico klez Culloca
  • 26,308
  • 17
  • 56
  • 95
UnknownBeast
  • 979
  • 1
  • 6
  • 13
0
  1. Use DateTimeFormatter to create the required pattern so that you can parse the date string to LocalDate.
  2. Use Comparator to compare the DateFrequency objects on parsed date string.

Demo:

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

class DateFrequency {
    private String date;
    private String frequency;

    public DateFrequency(String date, String frequency) {
        this.date = date;
        this.frequency = frequency;
    }

    public String getDate() {
        return date;
    }

    @Override
    public String toString() {
        return "DateFrequency [date=" + date + ", frequency=" + frequency + "]";
    }
}

public class Main {
    public static void main(String[] args) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");

        List<DateFrequency> list = new ArrayList<DateFrequency>();
        list.add(new DateFrequency("05/10/2020", "60-DAYS"));
        list.add(new DateFrequency("05/10/2020", "30-DAYS"));
        list.add(new DateFrequency("05/11/2020", "30-DAYS"));
        list.add(new DateFrequency("05/12/2020", "60-DAYS"));
        list.add(new DateFrequency("05/11/2020", "90-DAYS"));

        // Sort on dates in ascending order
        Collections.sort(list, Comparator.comparing((df) -> LocalDate.parse(df.getDate(), formatter)));

        // Display the sorted result
        for (DateFrequency df : list) {
            System.out.println(df);
        }
    }
}

Output:

DateFrequency [date=05/10/2020, frequency=60-DAYS]
DateFrequency [date=05/10/2020, frequency=30-DAYS]
DateFrequency [date=05/11/2020, frequency=30-DAYS]
DateFrequency [date=05/11/2020, frequency=90-DAYS]
DateFrequency [date=05/12/2020, frequency=60-DAYS]

Assumptions:

  1. The date strings are in the format dd/MM/yyyy. If not, change it in the object, formatter accordingly.
  2. The sorting is required only on dates in ascending order.
Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110