comparator

How to use Comparator in Java to sort

自古美人都是妖i 提交于 2019-11-25 23:59:03
问题 I learned how to use the comparable but I\'m having difficulty with the Comparator. I am having a error in my code: Exception in thread \"main\" java.lang.ClassCastException: New.People cannot be cast to java.lang.Comparable at java.util.Arrays.mergeSort(Unknown Source) at java.util.Arrays.sort(Unknown Source) at java.util.Collections.sort(Unknown Source) at New.TestPeople.main(TestPeople.java:18) Here is my code: import java.util.Comparator; public class People implements Comparator {

“Comparison method violates its general contract!”

怎甘沉沦 提交于 2019-11-25 23:57:07
问题 Can someone explain me in simple terms, why does this code throw an exception, \"Comparison method violates its general contract!\", and how do I fix it? private int compareParents(Foo s1, Foo s2) { if (s1.getParent() == s2) return -1; if (s2.getParent() == s1) return 1; return 0; } 回答1: Your comparator is not transitive. Let A be the parent of B , and B be the parent of C . Since A > B and B > C , then it must be the case that A > C . However, if your comparator is invoked on A and C , it

When should a class be Comparable and/or Comparator?

喜夏-厌秋 提交于 2019-11-25 23:36:12
问题 I have seen classes which implement both Comparable and Comparator . What does this mean? Why would I use one over the other? 回答1: The text below comes from Comparator vs Comparable Comparable A comparable object is capable of comparing itself with another object. The class itself must implements the java.lang.Comparable interface in order to be able to compare its instances. Comparator A comparator object is capable of comparing two different objects. The class is not comparing its instances

Java error: Comparison method violates its general contract

徘徊边缘 提交于 2019-11-25 22:43:40
问题 I saw many questions about this, and tried to solve the problem, but after one hour of googling and a lots of trial & error, I still can\'t fix it. I hope some of you catch the problem. This is what I get: java.lang.IllegalArgumentException: Comparison method violates its general contract! at java.util.ComparableTimSort.mergeHi(ComparableTimSort.java:835) at java.util.ComparableTimSort.mergeAt(ComparableTimSort.java:453) at java.util.ComparableTimSort.mergeForceCollapse(ComparableTimSort.java

Sort ArrayList of custom Objects by property

两盒软妹~` 提交于 2019-11-25 21:33:38
问题 I read about sorting ArrayLists using a Comparator but in all of the examples people used compareTo which according to some research is a method for Strings. I wanted to sort an ArrayList of custom objects by one of their properties: a Date object ( getStartDay() ). Normally I compare them by item1.getStartDate().before(item2.getStartDate()) so I was wondering whether I could write something like: public class CustomComparator { public boolean compare(Object object1, Object object2) { return

How does Javascript's sort() work?

烂漫一生 提交于 2019-11-25 21:02:45
How does the following code sort this array to be in numerical order? var array=[25, 8, 7, 41] array.sort(function(a,b){ return a - b }) I know that if the result of the computation is... Less than 0 : "a" is sorted to be a lower index than "b". Zero: "a" and "b" are considered equal, and no sorting is performed. Greater than 0: "b" is sorted to be a lower index than "a". Is the array sort callback function called many times during the course of the sort? If so, I'd like to know which two numbers are passed into the function each time. I assumed it first took "25"(a) and "8"(b), followed by "7