I am getting error below:
Caused by: javax.faces.el.EvaluationException: java.lang.IllegalArgumentException: Comparison method violates its general contract!
at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:101) [jboss-jsf-api_2.1_spec-2.1.28.SP1-redhat-1.jar:2.1.28.SP1-redhat-1]
at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:101) [jsf-impl-2.1.28.redhat-10.jar:2.1.28.redhat-10]
at javax.faces.component.UICommand.broadcast(UICommand.java:315) [jboss-jsf-api_2.1_spec-2.1.28.SP1-redhat-1.jar:2.1.28.SP1-redhat-1]
at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:786) [jboss-jsf-api_2.1_spec-2.1.28.SP1-redhat-1.jar:2.1.28.SP1-redhat-1]
at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1251) [jboss-jsf-api_2.1_spec-2.1.28.SP1-redhat-1.jar:2.1.28.SP1-redhat-1]
at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81) [jsf-impl-2.1.28.redhat-10.jar:2.1.28.redhat-10]
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101) [jsf-impl-2.1.28.redhat-10.jar:2.1.28.redhat-10]
at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118) [jsf-impl-2.1.28.redhat-10.jar:2.1.28.redhat-10]
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:593) [jboss-jsf-api_2.1_spec-2.1.28.SP1-redhat-1.jar:2.1.28.SP1-redhat-1]
... 29 more
Caused by: java.lang.IllegalArgumentException: Comparison method violates its general contract!
at java.util.TimSort.mergeHi(TimSort.java:899) [rt.jar:1.8.0_65]
at java.util.TimSort.mergeAt(TimSort.java:516) [rt.jar:1.8.0_65]
at java.util.TimSort.mergeForceCollapse(TimSort.java:457) [rt.jar:1.8.0_65]
at java.util.TimSort.sort(TimSort.java:254) [rt.jar:1.8.0_65]
at java.util.Arrays.sort(Arrays.java:1512) [rt.jar:1.8.0_65]
at java.util.ArrayList.sort(ArrayList.java:1454) [rt.jar:1.8.0_65]
at java.util.Collections.sort(Collections.java:175) [rt.jar:1.8.0_65]
Below is my code for compare method. vo1.getAttribute()
returns java.util.Date
Object.
@Override
public int compare(DateComparableVO vo1, DateComparableVO vo2) {
if (vo1 != null && vo1.getAttribute() != null && vo2 != null && vo2.getAttribute() != null) {
return vo1.getAttribute().compareTo(vo2.getAttribute());
}
return -1;
}
Is there anything wrong with my implementation of compare method?
In case of null scenario.
Why below code works without any issue.
package test;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
public class TestMain {
public static void main(String[] args) {
List<Employee> employees = new ArrayList<Employee>();
employees.add(new Employee(new Date()));
employees.add(null);
employees.add(new Employee(new Date()));
employees.add(new Employee(new Date()));
employees.add(null);
employees.add(new Employee(new Date()));
employees.add(null);
System.out.println(employees.size());
Collections.sort(employees, new EmployeeComparator());
}
}
class Employee {
private Date attribute;
public Employee() {
// TODO Auto-generated constructor stub
}
public Employee(Date attribute) {
this.attribute = attribute;
}
public Date getAttribute() {
return attribute;
}
public void setAttribute(Date attribute) {
this.attribute = attribute;
}
@Override
public String toString() {
return "Employee [attribute=" + attribute + "]";
}
}
class EmployeeComparator implements Comparator<Employee>{
@Override
public int compare(Employee vo1, Employee vo2) {
System.out.println("VO1 : " + vo1 + " VO2 : " + vo2);
if (vo1 != null && vo1.getAttribute() != null && vo2 != null && vo2.getAttribute() != null) {
return vo1.getAttribute().compareTo(vo2.getAttribute());
}
return -1;
}
}
Output
7
VO1 : null VO2 : Employee [attribute=Fri Aug 18 11:51:11 SGT 2017]
VO1 : Employee [attribute=Fri Aug 18 11:51:11 SGT 2017] VO2 : null
VO1 : Employee [attribute=Fri Aug 18 11:51:11 SGT 2017] VO2 : Employee [attribute=Fri Aug 18 11:51:11 SGT 2017]
VO1 : Employee [attribute=Fri Aug 18 11:51:11 SGT 2017] VO2 : null
VO1 : Employee [attribute=Fri Aug 18 11:51:11 SGT 2017] VO2 : Employee [attribute=Fri Aug 18 11:51:11 SGT 2017]
VO1 : null VO2 : null
VO1 : null VO2 : Employee [attribute=Fri Aug 18 11:51:11 SGT 2017]
VO1 : null VO2 : Employee [attribute=Fri Aug 18 11:51:11 SGT 2017]
VO1 : Employee [attribute=Fri Aug 18 11:51:11 SGT 2017] VO2 : Employee [attribute=Fri Aug 18 11:51:11 SGT 2017]
VO1 : Employee [attribute=Fri Aug 18 11:51:11 SGT 2017] VO2 : Employee [attribute=Fri Aug 18 11:51:11 SGT 2017]
VO1 : null VO2 : null
VO1 : null VO2 : Employee [attribute=Fri Aug 18 11:51:11 SGT 2017]
VO1 : null VO2 : null
Am I missing some important part in understanding compare