comparator

Java thenComparing wildcard signature

牧云@^-^@ 提交于 2021-02-17 19:14:27
问题 Why does the declaration look like this: default <U extends Comparable<? super U>> Comparator<T> thenComparing( Function<? super T, ? extends U> keyExtractor) I understand most of it. It makes sense that U can be anything as long as it's comparable to a superclass of itself, and thus also comparable to itself. But I don't get this part: Function<? super T, ? extends U> Why not just have: Function<? super T, U> Can't the U just parameterize to whatever the keyExtractor returns, and still

Java thenComparing wildcard signature

社会主义新天地 提交于 2021-02-17 19:14:01
问题 Why does the declaration look like this: default <U extends Comparable<? super U>> Comparator<T> thenComparing( Function<? super T, ? extends U> keyExtractor) I understand most of it. It makes sense that U can be anything as long as it's comparable to a superclass of itself, and thus also comparable to itself. But I don't get this part: Function<? super T, ? extends U> Why not just have: Function<? super T, U> Can't the U just parameterize to whatever the keyExtractor returns, and still

java Comparator sorting by multiple fields

可紊 提交于 2021-02-16 15:45:50
问题 Need to sort class Dir by 3 String fields. First one: sort by 1 value - s1 ; Then sort by String length - s1+s2+s3 ; After sort by 2 value - s2 ; And sort by 3 value - s3 . In my code its works for value 1 and String length sorting, but sorting by 2 value does not works. How i can sorting class Dir by multiple values? Class Dir: public class Dir { private String s1 = ""; private String s2 = ""; private String s3 = ""; public Dir(String s1){ this.s1 = s1; } public Dir(String s1, String s2){

Comparator for vector<pair<int,int>> [duplicate]

我只是一个虾纸丫 提交于 2021-02-08 12:13:41
问题 This question already has answers here : How do I sort a vector of pairs based on the second element of the pair? (7 answers) Closed 6 years ago . vector<pair<int,int> > v; for(i=0;i<5;i++){ cin>>b>>c; v.push_back(make_pair(b,c)); } sort(v.begin(),v.end()); Is it possible to write a comparator for the sort function such that v[i].first is sorted in increasing order and for similar values of v[i].first , v[i].second is sorted in decreasing order? like:- i/p: 13 10 44 15 13 15 13 99 6 45 o/p: 6

Comparator for vector<pair<int,int>> [duplicate]

泄露秘密 提交于 2021-02-08 12:12:33
问题 This question already has answers here : How do I sort a vector of pairs based on the second element of the pair? (7 answers) Closed 6 years ago . vector<pair<int,int> > v; for(i=0;i<5;i++){ cin>>b>>c; v.push_back(make_pair(b,c)); } sort(v.begin(),v.end()); Is it possible to write a comparator for the sort function such that v[i].first is sorted in increasing order and for similar values of v[i].first , v[i].second is sorted in decreasing order? like:- i/p: 13 10 44 15 13 15 13 99 6 45 o/p: 6

Sort a list with known values before unknown values

心不动则不痛 提交于 2021-02-07 19:30:17
问题 I'm trying to sort a list with the following rules: Known values should be ordered before unknown values. Known values should be ordered by a separately defined key. Unknown values should be ordered by their natural ordering. I've got (1) and (2), just struggling with adding (3) in to the mix. So far I have this: List<String> values = Arrays.asList( "red", "orange", "yellow", "green", "blue", "indigo", "violet"); ImmutableMap<String, Integer> map = ImmutableMap.of("red", 1, "green", 2, "blue"

sorting strings in Java based on substrings

只谈情不闲聊 提交于 2021-02-07 18:43:44
问题 I have a list of strings def123, abc999, zzz000, abc123, zzz111 . I want the list sorted such that first three characters are sorted in ascendng order and next three in descending. So the output should be abc999, abc123, def123, zzz111,zzz000 Is this possible? 回答1: Other answers have suggested you implement Comparator . That's no longer necessary with recent utility methods added to the interface in Java 8: list.sort(Comparator .comparing(s -> s.substring(0, 3)) .thenComparing(s -> s.subtring

Java Method reference not expected here

爷,独闯天下 提交于 2021-02-07 14:26:28
问题 How exactly do you chain method references for instances with Java 8? Example: Collections.sort(civs,Comparator.comparing(Civilization::getStrategy.getStrategLevel)); getStrategy of a Civilization instance returns a Strategy object instance which has the instance method getStrategyLevel . Why doesn't the Comparator.comparing method return a comparator with it's functional interface implemented by the lambda expression? 回答1: In that case, you should use a lambda, you can't apply a method

How to handle comparison operator for Generic Java class accepting String or Integer

♀尐吖头ヾ 提交于 2021-02-07 07:52:56
问题 I am interested in using a generic Java class which can handle String or Integer data type. I'd like to utilize the comparison operator, but it appears to have a compile error: Operator < cannot be used on generic T How can I achieve using comparison operators such as <, >, =, etc. for T ? (Assuming T is Number or String ). public class Node<T> { public T value; public Node(){ } public void testCompare(T anotherValue) { if(value == anotherValue) System.out.println("equal"); else if(value <

How to make a map sort by value C++

这一生的挚爱 提交于 2021-02-05 12:23:07
问题 I was trying to make a map sort by value using a custom comparator but I couldn't figure out why I kept getting the error of "no matching call to compareByVal" Here's what I had in my main.cpp: #include <map> #include <iostream> struct compareByVal { bool operator[](const std::pair<int,int> & a, const std::pair<int,int> & b) return a.second < b.second; } int main() { std::map<int,int,compareByVal> hash; hash[1] = 5; hash[2] = 2; hash[3] = 10; std::cout << hash.begin()->first << std::endl; }