comparator

STL Priority Queue on custom class

与世无争的帅哥 提交于 2019-11-28 05:06:05
I'm having a lot of trouble getting my priority queue to recognize which parameter it should sort by. I've overloaded the less than operator in my custom class but it doesn't seem to use it. Here's the relevant code: Node.h class Node { public: Node(...); ~Node(); bool operator<(Node &aNode); ... } Node.cpp #include "Node.h" bool Node::operator<(Node &aNode) { return (this->getTotalCost() < aNode.getTotalCost()); } getTotalCost() returns an int main.cpp priority_queue<Node*, vector<Node*>,less<vector<Node*>::value_type> > nodesToCheck; What am I missing and/or doing wrong? rlbond less<vector

Java comparator for String in EBCDIC encoding

亡梦爱人 提交于 2019-11-28 04:45:28
问题 I have come across a requirement where I need to convert a string to EBCDIC encoding and then sort it. We need to sort it with EBCDIC because the string has to go in mainframe. The string I will sort will have only alphabets in captial and integers only. I googled it some and then I came across the link from IBM which has listed the characters in order What I realized was that EBCDIC sorting is exactly opposite to normal java lexicographic sorting (at least for the type of data which I am

Create a SortedMap in Java with a custom Comparator

笑着哭i 提交于 2019-11-27 23:30:17
I want to create a TreeMap in Java with a custom sort order. The sorted keys which are string need to be sorted according to the second character. The values are also string. Sample map: Za,FOO Ab,Bar polygenelubricants You can use a custom comparator like this: Comparator<String> secondCharComparator = new Comparator<String>() { @Override public int compare(String s1, String s2) { return s1.substring(1, 2).compareTo(s2.substring(1, 2)); } }; Sample: SortedMap<String,String> map = new TreeMap<String,String>(secondCharComparator); map.put("Za", "FOO"); map.put("Ab", "BAR"); map.put("00", "ZERO"

how to sort an ArrayList in ascending order using Collections and Comparator

雨燕双飞 提交于 2019-11-27 23:16:47
How to sort an ArrayList in ascending order using Comparator ? I know how to sort it in descending order using: Comparator mycomparator = Collections.reverseOrder(); then Collections.sort(myarrayList,mycomparator); just want to know how to sort it in ascending order using Collections and comparator? Thanks! Just throwing this out there...Can't you just do: Collections.sort(myarrayList); It's been awhile though... Use the default version: Collections.sort(myarrayList); Of course this requires that your Elements implement Comparable , but the same holds true for the version you mentioned. BTW:

Java: SortedMap, TreeMap, Comparable? How to use?

こ雲淡風輕ζ 提交于 2019-11-27 22:33:22
I have a list of objects I need to sort according to properties of one of their fields. I've heard that SortedMap and Comparators are the best way to do this. Do I implement Comparable with the class I'm sorting, or do I create a new class? How do I instantiate the SortedMap and pass in the Comparator? How does the sorting work? Will it automatically sort everything as new objects are inserted? EDIT: This code is giving me an error: private TreeMap<Ktr> collection = new TreeMap<Ktr>(); (Ktr implements Comparator<Ktr> ). Eclipse says it is expecting something like TreeMap<K, V> , so the number

Why does the Java Collections Framework offer two different ways to sort?

我只是一个虾纸丫 提交于 2019-11-27 21:41:42
If I have a list of elements I would like to sort, Java offers two ways to go about this. For example, lets say I have a list of Movie objects and I’d like to sort them by title. One way I could do this is by calling the one-argument version of the static java.util.Collections.sort( ) method with my movie list as the single argument. So I would call Collections.sort(myMovieList). In order for this to work, the Movie class would have to be declared to implement the java.lang.Comparable interface, and the required method compareTo( ) would have to be implemented inside this class. Another way to

compare object properties and show diff in PHP

瘦欲@ 提交于 2019-11-27 21:20:30
I'm searching for a way to show me the different properties/values from given objects... $obj1 = new StdClass; $obj1->prop = 1; $obj2 = new StdClass; $obj2->prop = 2; var_dump(array_diff((array)$obj1, (array)$obj2)); //output array(1) { ["prop"]=> int(1) } This works very well as long the property is not a object or array. $obj1 = new StdClass; $obj1->prop = array(1,2); $obj2 = new StdClass; $obj2->prop = array(1,3); var_dump(array_diff((array)$obj1, (array)$obj2)) // Output array(0) { } // Expected output - array { ["prop"]=> array { [1]=> int(2) } } Is there a way to get rid of this, even

Making a Java PriorityQueue into a stable priority queue

故事扮演 提交于 2019-11-27 19:25:11
问题 I'm trying to implement a stable (first in first out) priority queue in Java. Supposing that the key is a name and the value is an age, I know I can make an unstable priority queue like this: Queue<Map.Entry<String, Integer>> pq = new PriorityQueue<Map.Entry<String, Integer>>(100, ageComparator); This does pretty much everything that I need it to, except that it doesn't maintain order of key-value pairs as I insert them (or remove them). I've found a "work around" by making a LinkedList,

comparator with null values

折月煮酒 提交于 2019-11-27 18:05:55
We have some code wich sorts a list of addresses based on the distance between their coordinates. this is done through collections.sort with a custom comparator. However from time to time an adress without coordinates is in the list causing a NullPointerException. My initial idea to fix this was to have the comparator return 0 as dististance for addresses where at least one of the coordinates is null. I fear this might lead to corruption of the order the 'valid' elements in the list. so is returning a '0' values for null data in a comparator ok, or is there a cleaner way to resolve this.

How to sort ArrayList using Comparator? [duplicate]

北城余情 提交于 2019-11-27 14:25:42
This question already has an answer here: Sort ArrayList of custom Objects by property 25 answers I have a Class Student that Implements a static method public static Comparator<Student> getCompByName() that returns a new comparator object for Student that compares 2 Students objects by the attribute 'name'. I need to now test this by sorting a students ArrayList by 'name' using my function getCompByName(). Here is my Comparator method in my Student class. public static Comparator<Student> getCompByName() { Comparator comp = new Comparator<Student>(){ @Override public int compare(Student s1,