comparator

Reverse a comparator in Java 8

筅森魡賤 提交于 2019-11-26 11:11:53
问题 I have an ArrayList and want sort it in descending order. I use for it java.util.stream.Stream.sorted(Comparator) method. Here is a description according Java API: Returns a stream consisting of the elements of this stream, sorted according to the provided Comparator . this methods return me a sort with ascending order. Which parameter should I change, just to have the descending order? 回答1: You can use Comparator.reverseOrder() to have a comparator giving the reverse of the natural ordering.

Comparator with double type

蹲街弑〆低调 提交于 2019-11-26 11:11:19
问题 I have written the following code: public class NewClass2 implements Comparator<Point> { public int compare(Point p1, Point p2) { return (int)(p1.getY() - p2.getY()); } } If I let\'s say have two double numbers, 3.2 - 3.1 , the difference should be 0.1 . When I cast the number to an int, however, the difference ends up as 0 , which is not correct. I therefore need compare() to return a double, not an int. The problem is, my getX field is a double. How can I solve this problem? 回答1: You don't

Sorting Java objects using multiple keys

馋奶兔 提交于 2019-11-26 10:25:53
I've got a collection of Duck objects and I'd like to sort them using multiple keys . class Duck { DuckAge age; //implements Comparable DuckWeight weight; //implements Comparable String name; } List<Duck> ducks = Pond.getDucks(); eg. I want to sort them primarily by their weights , and secondarily by their age . If two ducks have the exact same weight and the exact same age, then let's differentiate them using their names as a tertiary key . I might do something like this: Collections.sort(ducks, new Comparator<Duck>(){ @Override public int compare(Duck d1, Duck d2){ int weightCmp = d1.weight

PriorityQueue.toString wrong element order

ε祈祈猫儿з 提交于 2019-11-26 10:01:46
问题 I am trying to make a priority queue in java with the nodes with the lowest frequency in priority. However, my comparator is not working and the output is very weird. I believe I need to change my comparator but I am not sure how to change it. Here is my code: public class HuffmanComparator implements Comparator<TreeNodeHuffman> { public int compare(TreeNodeHuffman p1, TreeNodeHuffman p2) { if (p1.frequency < p2.frequency) return -1; if (p1.frequency > p2.frequency) return 1; return 0; } }

Java TreeMap Comparator

拈花ヽ惹草 提交于 2019-11-26 05:33:31
问题 I need a comparator for a TreeMap. Should I write this anonymously in the constructor for my TreeMap? How else could I write my comparator. Currently, Java does not like my code (can I do this anonymously?): SortedMap<String, Double> myMap = new TreeMap<String, Double>(new Comparator<Entry<String, Double>>() { public int compare(Entry<String, Double> o1, Entry<String, Double> o2) { return o1.getValue().compareTo(o2.getValue()); } }); Can I do the above anonymously? How else could I do this? I

Very confused by Java 8 Comparator type inference

梦想与她 提交于 2019-11-26 03:37:52
问题 I\'ve been looking at the difference between Collections.sort and list.sort , specifically regarding using the Comparator static methods and whether param types are required in the lambda expressions. Before we start, I know I could use method references, e.g. Song::getTitle to overcome my problems, but my query here is not so much something I want to fix but something I want an answer to, i.e. why is the Java compiler handling it in this way. These are my finding. Suppose we have an

Natural sort order string comparison in Java - is one built in? [duplicate]

半腔热情 提交于 2019-11-26 03:20:47
问题 This question already has answers here : Sort on a string that may contain a number (21 answers) Closed 2 years ago . I\'d like some kind of string comparison function that preserves natural sort order 1 . Is there anything like this built into Java? I can\'t find anything in the String class, and the Comparator class only knows of two implementations. I can roll my own (it\'s not a very hard problem), but I\'d rather not re-invent the wheel if I don\'t have to. In my specific case, I have

When to use Comparable and Comparator

只谈情不闲聊 提交于 2019-11-26 01:38:49
问题 I have a list of objects I need to sort on a field, say Score. Without giving much thought I wrote a new class that implements Comparator, that does the task and it works. Now looking back at this, I am wondering if I should have instead have the my class implement Comparable instead of creating a new class that implements Comparator. The score is the only field that the objects will be ordered on. What I have done acceptable as a practice? Is the right approach \"First have the class

How does Javascript&#39;s sort() work?

北城以北 提交于 2019-11-26 01:35: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

Sorting Java objects using multiple keys

安稳与你 提交于 2019-11-26 01:15:55
问题 I\'ve got a collection of Duck objects and I\'d like to sort them using multiple keys . class Duck { DuckAge age; //implements Comparable DuckWeight weight; //implements Comparable String name; } List<Duck> ducks = Pond.getDucks(); eg. I want to sort them primarily by their weights , and secondarily by their age . If two ducks have the exact same weight and the exact same age, then let\'s differentiate them using their names as a tertiary key . I might do something like this: Collections.sort