comparator

compare list with multiple attributes involving a boolean

浪尽此生 提交于 2019-12-21 05:45:08
问题 I have some classes that implement the comparator interface to sort an ArrayList by adding patient objects, I want to sort the list by multiple attributes and have no problem sorting with just Enums, however I want to override this sort by sorting with a boolean. I know I cannot use the compareTo method as it is not a Wrapper class but I am not able to find a suitable way to sort the list via boolean . Any help would be greatly appreciated. public Patient(int nhsNumber, String name, Status

Why is it not necessary to override both methods of interface Comparator in Java

懵懂的女人 提交于 2019-12-21 02:46:30
问题 We know that it is necessary to implement all methods of an interface, if we want to make an object of that class. But why is it not necessary to implement both the methods compare() and equals() of interface Comparator in java? I agree that the purpose is solved but even then why is it not mandatory to override equals () if we override compare ()? 回答1: Since all classes implicitly extend Object every implementation of a Comparator has an equals method, because every Object has one. It would

Comparator for min-heap in C++

流过昼夜 提交于 2019-12-20 12:06:23
问题 I am trying to make a min-heap 1 of long s in C++ using the STL make_heap , etc., but my comparator doesn't seem to be comparing properly. The following is my current comparator: struct greater1{ bool operator()(const long& a,const long& b) const{ return a>b; } }; However, when I do std::pop_heap(humble.begin(),humble.end(),g); where g is an instance of greater1 and humble is a heap who makes [9,15,15,25] when sort_heap is called, I get a 15 popped. Is my comparator correct? what might be

Java 8 Lambda: Comparator

六眼飞鱼酱① 提交于 2019-12-20 09:56:14
问题 I want to sort a list with Lambda: List<Message> messagesByDeviceType = new ArrayList<Message>(); messagesByDeviceType.sort((Message o1, Message o2)->o1.getTime()-o2.getTime()); But I got this compilation error: Multiple markers at this line - Type mismatch: cannot convert from long to int - The method sort(Comparator<? super Message>) in the type List<Message> is not applicable for the arguments ((Message o1, Message o2) -> {}) 回答1: Comparator#compareTo returns an int ; while getTime is

Wrong order in java.util.PriorityQueue and specific Comparator

爱⌒轻易说出口 提交于 2019-12-20 07:47:14
问题 i am very confused with this little example of java.util.PriorityQueue and my own Comparator: In this code i get a wrong order in the queue. The result is: 5,8,7 instead of 5,7,8 Is there anything wrong with my Comparator<Vertex> ? Thank you for your help. public class Test { public static void main(String[] args) { PriorityQueue<Vertex> priorityQueue = new PriorityQueue<Vertex>(new Comparator() { @Override public int compare(Object o1, Object o2) { Vertex u = (Vertex) o1; Vertex v = (Vertex)

Java Long Compare and ValueOf method undefined

佐手、 提交于 2019-12-20 04:26:09
问题 I am referencing my java version JDK 1.8 but I am still getting error. What is wrong with this referencing (writing Java after 6 years)? or any other simpler way to do this? I did some search and these functions are available in later java versions. Eclipse is Oxygen The method valueOf(Long) is undefined for the type Long The method compareTo() is undefined for the type Long import java.util.Comparator; import java.lang.Long; public class MyComparator<Long> implements Comparator<Long>{

Java - how to sort object in many ways: Arrays.sort(), Comparable<T>

筅森魡賤 提交于 2019-12-19 19:55:05
问题 Let's say that I have an array with objects, where I have some employees (objects). They all have: int age , double salary . I want to sort this array so my class implements Comparable <Employee> . I've made a method: public int compareTo(Employee other) { return Double.compare(salary, other.salary); } And it's ok, sorting is working fine. But I'm sorting by double salary . Now I want to sort by int age so what now? I've made a method: public int compareAge(Employee other) { return Integer

Java Comparator for Objects with multiple fields [closed]

狂风中的少年 提交于 2019-12-19 05:10:35
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 5 years ago . I have an Object Collection with 5 fields: id; entityType; entityId; brandId; productId; To sort an ArrayList of Collection I have written the following Comparaor . Comparator<Collection> collectionComparator = new Comparator<Collection>() { @Override public int compare(Collection collection1, Collection

Java Comparator for Objects with multiple fields [closed]

泄露秘密 提交于 2019-12-19 05:10:18
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 5 years ago . I have an Object Collection with 5 fields: id; entityType; entityId; brandId; productId; To sort an ArrayList of Collection I have written the following Comparaor . Comparator<Collection> collectionComparator = new Comparator<Collection>() { @Override public int compare(Collection collection1, Collection

Sort two arrayLists concurrently

[亡魂溺海] 提交于 2019-12-19 04:48:12
问题 Say I have two ArrayLists: name: [Four, Three, One, Two] num: [4, 3, 1, 2] If I do: Arrays.sort(num), then I have: name: [Four, Three, One, Two] num: [1, 2, 3, 4] Is there any way I can possibly do a sort on num and have it reflected in name as well, so that I might end up with: name: [One, Two, Three, Four] num: [1, 2, 3, 4] ? Please do help me out. I thought of Comparators and Objects, but barely know them at all. 回答1: You should somehow associate name and num fields into one class and then