2

I'm trying to sort a list of objects in java using Collections.sort(). But I keep getting this error: type parameter is not within its bound". Does anyone know how I can remedy this problem?

My code

   public List<String> fetchNumbersForLeastContacted()
   {


    List<String> phonenumberList = getUniquePhonenumbers();
    List<TopTen> SortList = new ArrayList<TopTen>();


    Date now = new Date();
    Long milliSeconds = now.getTime();

    //Find phone numbers for least contacted
    for (String phonenumber : phonenumberList)
    {



       int outgoingSMS = fetchSMSLogsForPersonToDate(phonenumber, milliSeconds).getOutgoing();
       int outgoingCall = fetchCallLogsForPersonToDate(phonenumber, milliSeconds).getOutgoing();

       //Calculating the total communication for each phone number
       int totalCommunication = outgoingCall + outgoingSMS;

       android.util.Log.i("Datamodel", Integer.toString(totalCommunication));

       SortList.add(new TopTen(phonenumber, totalCommunication, 0));

    }

    //This is where I get the error
   Collections.sort(SortList);

The TopTen.class

public class TopTen {

private String phonenumber;
private int outgoing;
private int incoming;


public TopTen (String phonenumber, int outgoing, int incoming)
{
    this.phonenumber = phonenumber;
    this.incoming = incoming;
    this.outgoing = outgoing;


}

public String getPhonenumber() {
    return phonenumber;
}

public void setPhonenumber(String phonenumber) {
    this.phonenumber = phonenumber;
}

public int getOutgoing() {
    return outgoing;
}

public void setOutgoing(int outgoing) {
    this.outgoing = outgoing;
}

public int getIncoming() {
    return incoming;
}

public void setIncoming(int incoming) {
    this.incoming = incoming;
}}

2 Answers 2

2
public static void sort (List<T> list)

This method can only be used if T inplements the Comparable interface. What implements Comparable means is that there exists a criteria by which two objects of type T can be compared and ordered. In your case, T is TopTen, which does not implement Comparable.

What you need to do:

public class TopTen  implements Comparator<TopTen> {

    ....
    ....

    @Override
    public int compareTo(TopTen other) {

        if (this == other) return EQUAL;

        return this.getPhonenumber().compareToIgnoreCase(other.getPhonenumber());

    }

This will compare two TopTen objects based on the phonenumber field. If you want the objects to be ordered based on another criteria, use that to return either -1 (before), 0 (equal) or 1 (after).

For example, to base the sorting on incoming, use the following:

@Override
public int compareTo(TopTen other) {

    final int BEFORE = -1;
    final int EQUAL = 0;
    final int AFTER = 1;

    if (this == other) return 0;

    if (this.getIncoming() > other.getIncoming()) {
        return AFTER;
    } else if (this.getIncoming() < other.getIncoming()) {
        return BEFORE;
    } else {
        return EQUAL;
    }

}

This would get you TopTen objects ordered by ascending incoming field values.

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

Comments

0

try implementing Comparable interface in TopTen class and override compareTo method to specify your sorting logic

@Override
public int compareTo(TopTen o) {
    // TODO Auto-generated method stub
    return 0;
}

(or)

Collections.sort(SortList, new Comparator<TopTen>(){
            public int compare(TopTen t1, TopTen t2) {
                return t1.phonenumber.compareTo(t2.phonenumber);
            }
        });

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.