Sort AbsoluteDates in chronological order


As the title suggests, I was wondering if there is a quick way to sort a list of AbsoluteDates in chronological order (unfortunately, I’m not able to directly generate them as I wish).

P.S. My current solution is to implement the “isAfterOrEqualTo” method inside a sorting algorithm that iterates through the dates, but - since I’m working with very large arrays - maybe there is a quicker solution that I didn’t see!

Thank you

Hi @misanthrope

You can use a TreeSet defined with a ChronologicalComparator. Please find below an example.

    final SortedSet<TimeStamped> dates = new TreeSet<>(new ChronologicalComparator());
    dates.add(new AbsoluteDate(2017, 7, 8, TimeScalesFactory.getUTC()));
    dates.add(new AbsoluteDate(2016, 7, 8, TimeScalesFactory.getUTC()));
    dates.add(new AbsoluteDate(2018, 7, 8, TimeScalesFactory..getUTC()));

    // Print the dates to verify the order
    for (final TimeStamped date : dates) {

In the above example, the dates are printed in chronological order.

Best regards,

If you don’t want a Set, maybe you want duplicated dates in your list, you can also simply use the sort method from the List class such as:

List<AbsoluteDate> dates =  new ArrayList<>();
... // Populates the list of dates
dates.sort(new ChronologicalComparator()); // the dates are then chronologically sorted