1

I'm trying to arrange an array of movies in alphabetical order, ignoring case. My application allows the user to add his/her own entry into an already initialized array of movies in a ListView. Here is the snippet:

            Lab7_082588FetchDetails newMovie = new Lab7_082588FetchDetails();
            IgnoreCaseComparator ignoreCase = new IgnoreCaseComparator();
            newMovie.setTitle(data.getStringExtra(Lab7_082588Edit.TITLE_STRING));
            newMovie.setGross(data.getStringExtra(Lab7_082588Edit.GROSS_STRING));
            newMovie.setDate(data.getStringExtra(Lab7_082588Edit.DATE_STRING));
            results.add(newMovie);
            adapter.notifyDataSetChanged();
            Collections.sort(results, ignoreCase);

However the .sort portion of the last line gives an error message:

The method sort(List<T>, Comparator<? super T>) in the type Collections is not applicable for the arguments (ArrayList<Lab7_082588FetchDetails>, IgnoreCaseComparator)

Also, this is my IgnoreCaseComparator:

import java.util.Comparator;

class IgnoreCaseComparator implements Comparator<String> {
    public int compare(String strA, String strB) {
        return strA.compareToIgnoreCase(strB);
    }  
}

And my Fetch Details Class:

public class Lab7_082588FetchDetails implements Comparable<Lab7_082588FetchDetails> {

private String title;
private String gross;
private String date;

public String getTitle() {
    return title;
}

public void setTitle(String title) {
    this.title = title;
}

public String getGross() {
    return gross;
}

public void setGross(String gross) {
    this.gross = gross;
}

public String getDate() {
    return date;
}

public void setDate(String date) {
    this.date = date;
}

@Override
public int compareTo(Lab7_082588FetchDetails another) {
    // TODO Auto-generated method stub
    return title.compareTo(another.title);
}

}

How do I remedy this?

2 Answers 2

2
implements Comparator<String>

Your comparator accepts String only, but your list has Lab7_082588FetchDetails object.

Change it to implements Comparator<Lab7_082588FetchDetails>

and change compare method to

public int compare(Lab7_082588FetchDetails objA, Lab7_082588FetchDetails objB) {
        return objA.getTitle().compareToIgnoreCase(objB.getTitle());
    }  
Sign up to request clarification or add additional context in comments.

3 Comments

I have tried changing Comparator to accept Objects, but it does a Force Close on me.
it works perfectly. I can't believe I never thought of this. Much appreciated help sir thinsteep!
Glad it helped. Enjoy coding.
2

Either use comparator or comparable

Comparable : when you can change class structure to add compareTo method

comparator: If you can't change class structure.

Also both should be passed with the object that needs to be compared. In your case comparator is written for String and you are passing Lab7_082588FetchDetails

Comments

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.