2

I've an ArrayList of objects.

ArrayList<Item> blog_titles = new ArrayList<Item>();

I want to sort the ArrayList in the descending order of one of the datamembers which is a DateTime value stored as String (timestamp in the code below).

public class BlogItem implements Item, Comparable<BlogItem> {

    public final String id;
    public final String heading;
    public final String summary;
    public final String description;
    public final String thumbnail;
    public final String timestamp;  // format:- 2013-02-05T13:18:56-06:00
    public final String blog_link;

    public BlogItem(String id, String heading, String summary, String description, String thumbnail, String timestamp, String blog_link) {      
        this.id = id;
        this.heading = heading;
        this.summary = summary;
        this.description = description;
        this.thumbnail = thumbnail;
        this.timestamp = timestamp;   // format:- 2013-02-05T13:18:56-06:00
        this.blog_link = blog_link;
    }

    @Override
    public int compareTo(BlogItem o) {
        // TODO Auto-generated method stub
        return this.timestamp.compareTo(o.timestamp);
    }

}

Item is a generic interface:

public interface Item { 
   // TODO Auto-generated method stub
}

Now when i'm trying to sort the ArrayList like:

Collections.sort(blog_titles);

I get the following error message:

Bound mismatch: The generic method sort(List<T>) of type Collections is not applicable for the arguments (ArrayList<Item>). The inferred type Item is not a valid substitute for the bounded parameter <T extends Comparable<? super T>>

How do i fix the above error & is this the correct approach to sort the ArrayList in this case ?

4
  • Refer this link <a href="stackoverflow.com/questions/7514451/…> It will help you to sort out type data. Commented May 15, 2013 at 11:38
  • I've implemented the same concept which is mentioned in the solution...however i'm getting a different error msg here Commented May 15, 2013 at 12:10
  • what type error you getting? Commented May 15, 2013 at 12:28
  • Bound mismatch: The generic method sort(List<T>)... basically that's the issue here Commented May 15, 2013 at 12:30

2 Answers 2

3

Your blog_titles list is a list of Item.

Item itself is not Comparable, while BlogItem is.

Either declare blog_titles as a ArrayList<BlogItem>, or make Item extend Comparable

Sign up to request clarification or add additional context in comments.

Comments

1
Try this..

Collections.sort(blog_titles, new Comparator<BlogItem>() {

        @Override
        public int compare(BlogItem lhs, BlogItem rhs) 
        {
            // TODO Auto-generated method stub
            return (int)(rhs.timestamp - lhs.timestamp);
        }
    });

1 Comment

@Sourav here in my answer timestamp's datatype is long.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.