santafebound santafebound -4 years ago 65
Java Question

The method sort(LineItem.FullLineItemComparator) is undefined for the type List<LineItem>

I'm rebuilding some code in Java 6. For the following I get a warning with red-underline in Eclipse:

List<LineItem> lineItemList = new ArrayList<LineItem>();
lineItemList.sort(new LineItem.FullLineItemComparator());


The method sort(LineItem.FullLineItemComparator) is undefined for the type List<LineItem>


At.
.sort(...
Eclipse recommends:

((Object) lineItemList).sort(new LineItem.FullLineItemComparator());


This doesn't remove the warning and the code does not compile.

FullLineItemComparator:

static class FullLineItemComparator implements Comparator<LineItem>

{
public int compare(LineItem c1, LineItem c2)
{
int result;
result = c1.initials.compareTo(c2.initials);
if (result != 0) {
return result;
}

result = c1.date.compareTo(c2.date);
if (result != 0) {
return result;
}

result = c1.timeStart.compareTo(c2.timeStart);
if (result != 0) {
return result;
}

result = c1.timeEnd.compareTo(c2.timeEnd);
if (result != 0) {
return -result;
}

return 0;
}
}


Do you see anything wrong with the code?

Answer Source

The proper way how to sort a list from JCF in java is to use a static method in Collections.

Collections.sort(lineItemList, new LineItem.FullLineItemComparator());
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download