comparator

IllegalArgumentException on Collections.sort() method

爷,独闯天下 提交于 2019-12-11 03:36:28
问题 I have comparator for strings, which are converted to date. When I pass this comparator to Collections.sort() method I get java.lang.IllegalArgumentException: Comparison method violates its general contract! . I have read some articles about this exception, but I don't really understand why this exception appears. Any idea ? private SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm"); Comparator<String> comparator = new Comparator<String>() { @Override public int compare(String o1

How to define a `std::set` sorting on another class data member?

旧街凉风 提交于 2019-12-11 03:07:10
问题 The code I tried, but doesn't work: class A { public: struct cmpr_t { bool operator() (int k1, int k2) { return mp[k1] < mp[k2]; // doesn't compile } }; map<int, int> mp; // storing key->value set<int, cmpr_t> ss; // just keys, ordered by corresponding value in mp }; I just want a map and also a set , the map stores data (key, value), and the set only contains the keys, and I want the set ordered by keys' corresponding values. So how to define the set? UPDATE Compiler error: In member

Pass a custom comparator through a function

回眸只為那壹抹淺笑 提交于 2019-12-11 03:03:59
问题 I have a class with a function MyClass::doStuff(std::vector<MyCustomData*> toSort) { ... in which I call std::sort(toSort.begin(), toSort.end(), MyClass::SortByZ()); myClass::SortByZ() is a custom comparator. Now this works but I would like to achieve the following: I have several classes, which should each have its own comparator functor to sort "MyCustomData". So e.g. Class1... should have class Class1 { struct SortData { bool operator ()(MyCustomData *lhs, MyCustomData *rhs) { return lhs-

How can I get away with an intransitive comparator?

做~自己de王妃 提交于 2019-12-11 02:48:19
问题 I have a Comparator<Foo> with the following comparison function: float d = o1.bar - o2.bar; if (Math.abs(d) <= 0.001) { return 0; } else { return d < 0 ? -1 : 1; // inline Math.copySign } Essentially, this is supposed to compare two Foo s based on their bar property unless the values are close enough, in which case they should be declared equal. (This is important because I'm doing another sort after this, on a different property.) Obviously, though, this is not a transitive comparator. If

Comparator double does not work

社会主义新天地 提交于 2019-12-11 02:28:49
问题 I have developed a program that creates an array of Book Objects and sorts them based on user input. The sorting options are author-title-pages-price, and all work but the price sort. Please help me find why I cannot sort doubles using Comparator... My SchoolTextBook Class: import java.util.Comparator; public class SchoolTextBook { private String author; private String title; private int pageCount; private String ISBN; private double price; public String getAuthor() { return author; } public

Making a comparator from an ordered container

寵の児 提交于 2019-12-11 00:15:26
问题 Given a list of objects, what is the cleanest way to create a functor object to act as a comparator, such that the comparator respects the ordering of the objects in the list. It is guaranteed that the objects in the list are unique, and the list contains the entire space of possible objects. For example, suppose we have: const std::vector<std::string> ordering {"dog", "cat", "mouse", "elephant"}; Now we want a function to act as a comparator, say for a map: using Comparator = std::function

Removing overloaded method in Java

旧城冷巷雨未停 提交于 2019-12-10 18:22:33
问题 There are 2 overloaded methods. Each of these methods converts a list of one type to a list of a different type. But the first method uses a comparator. class SomeClass { public static <T, G> List<G> toListOfNewType(List<T> inputList, Function<T, G> mapperFunction, Comparator<? super G> comparator) { return Stream.ofNullable(inputList) .flatMap(List::stream) .map(mapperFunction) .sorted(comparator) .collect(Collectors.toList()); } public static <T, G> List<G> toListOfNewType(List<T> inputList

Comparator interface's equals method, why it is always safe not to override Object.equals(Object)

橙三吉。 提交于 2019-12-10 16:46:00
问题 I'm currently studying the Comparator interface and noticed that in the documentation for Comparator's equals method, it states Note that it is always safe not to override Object.equals(Object) I have checked the implementation for the default equals method in Object class So with the default implementation of equals method, it simply checks whether two instances points to the same object because this == obj tests for reference equality. But what happen if I have two instances of Comparator ,

Does anyone know of a Java Comparators library?

烈酒焚心 提交于 2019-12-10 15:45:35
问题 I am after a foss library that includes many useful Comparator implementations, that is has lots of little boring comparators that are ready to go. Apache commons comparators reverse null first/null last chain natural transformer There are so many other useful reusable possibilities that arent available. whitespace ignoring whitespace normalizing number aware strings - eg "apple 10" > "apple 2". @SPF Ive included some psuedo code that shows how one can normalize and compare in one pass

Is it possible to change the comparator of a C++ std::set?

你离开我真会死。 提交于 2019-12-10 14:48:13
问题 I have a set of data which in some occasion I need to sort them in one way and some occasion in another way. For example, suppose the data set is a set of strings,{"abc", "dfg",...}. Sometimes I need to sort them in alphabetic order and sometimes by comparing their length. Initially I used std::set as a container of my data and implemented 2 comparators, hoping that I can change the comparator of the set on the fly, cause the data is huge and it's not a good idea to copy it from one set to