Jake Crystal
Jake Crystal

Reputation: 3

Sort date ListArray along with a second Array for the Title

I have this for sorting the dates:

Collections.sort(calDate, new Comparator<String>() { 
    DateFormat f = new SimpleDateFormat("E MMM d, yyyy h");
    @Override
    public int compare(String date1, String date2) {
        try {
            return f.parse(date1).compareTo(f.parse(date2));
        } catch (ParseException e) {
            throw new IllegalArgumentException(e);
        }
    }
});

The title and dates are getting pulled from a calendar rss feed. I have them saved in two different arrays (calDate, calTitle).

Based off the order the events are added to the calendar, the events won't be in order.

calDate holds Wed Feb 10, 2016 11:30am to 12:30pm, Wed Sep 30, 2015 11:30am to 12:30pm, Wed Sep 23, 2015 9:30pm to 10:30pm

calTitle holds Feb 10th Event, Sep 30th Event, Sep 23rd Event

I am trying to order them like this

calDate reordered: Wed Sep 23, 2015 9:30pm to 10:30pm, Wed Sep 30, 2015 11:30am to 12:30pm, Wed Feb 10, 2016 11:30am to 12:30pm

calTitle holds Sep 23rd Event, Sep 30th Event, Feb 10th Event

Upvotes: 0

Views: 61

Answers (1)

Keefe Roedersheimer
Keefe Roedersheimer

Reputation: 347

You need to build an association between title and date in order to sort by one field and maintain that association. The easiest thing to do is to create a new object with a title field and a date field then create a Comparator or implement Comparable. You probably have some other fields in the RSS feed that you would like to keep there as well.

Also, please don't parse a string inside compareTo, do it in the setter of the new class that you create.

Upvotes: 2

Related Questions