2013-05-15 3 views
2

У меня есть ArrayList объектов.Проблема сортировки ArrayList в Android

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

Я хочу, чтобы отсортировать ArrayList в порядке убывания одного из datamembers который является значением DateTime хранится в виде строки (метки времени в коде ниже).

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); 
    } 

} 

товара является общий интерфейс:

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

Теперь, когда я пытаюсь сортировать ArrayList как:

Collections.sort(blog_titles); 

Я получаю следующее сообщение об ошибке:

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>> 

Как исправить вышеуказанную ошибку & - это правильный подход к сортировке ArrayList в этом случае?

+0

См эту ссылку this Это поможет вам разобраться в данных типа. –

+0

Я реализовал ту же концепцию, о которой упоминается в решении ... но я получаю другую ошибку здесь – Sourav

+0

какой тип ошибки вы получаете? –

ответ

3

Ваш список blog_titles - это список Item.

Item сам не Comparable, а BlogItem есть.

Либо объявить blog_titles как ArrayList<BlogItem> или сделать Item расширить Comparable

+0

Большое спасибо за обмен информацией. – Narasimha

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); 
     } 
    }); 
+0

@Sourav здесь, в моем ответе timestamp's datatype длинный. – TheFlash

Смежные вопросы