comparator

Is there a built-in way to compare IEnumerable<T> (by their elements)?

巧了我就是萌 提交于 2019-11-29 16:11:08
I would like to compare lists of elements of a given type, to see which list is "bigger". new BuiltInComparer<IEnumerable<int>>().Compare( new[] {3,2,3}, new[] {1,2,3}) ...would return 1 new BuiltInComparer<IEnumerable<int>>().Compare( new[] {1,2,3}, new[] {1,2,4}) ...would return -1 etc Is there any such built in comparer? I don't think there's anything built into the framework - and as Eric says, you haven't provided the comparison criteria. If you mean "compare element-wise in the natural way, and assume a 'missing' element is smaller than any present element" (i.e. a longer sequence beats

A Mechanism for having different equals (physical equals and logical equals) on objects in Collection

北慕城南 提交于 2019-11-29 14:24:59
Is there any Equalator mechanism like Comparator so I can have different equals for coparing lists? EDIT: My goal is to differentiate between current list1.equals(list2) which checks if its a shallow copy or also a deep copy with all objects a.equals(b) and list1.identical(list2) which checks if its simply shallow copy with unmodified listing All these lists are from the same model. Some are copies of themselves so they hold the pointer to same objects, and others are deep copies so hierarchy is totally replicated, because they have updates in content, not just in structure. I find myself

Does Collections.sort keep order on equal elements?

北城以北 提交于 2019-11-29 13:14:31
I have a list of objects ordered by a date parameter and want to reorder them by category parameter, but keeping the date order within the category. Is something like this enough, or do I have to implement a comparator that takes on account the date for objects of the same category? // sort the list by category asc(, date asc ) Collections.sort((List<Object>)entries, new Comparator<Object>() { @Override public int compare(Object elementA, Object elementB) { return elementA.category.compareTo(elementB.category); // what happens when elementA.category.equals(elementB.category)? } }); The code in

Java comparator for String in EBCDIC encoding

戏子无情 提交于 2019-11-29 11:16:49
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 going to process). My question is my realization right ? If not what I am missing ? OR is there any java

sort a list of objects based on runtime property

拥有回忆 提交于 2019-11-29 09:42:51
问题 I have an arraylist of VOs. These objects have many properties and corresponding get/set methods. I want to sort this array list based on a property which I'll be getting in runtime. Let me explain in detail. My VO is like this public class Employee { String name; String id; private String getName() { return name; } private String getId() { return id; } } I will be getting a string ‘sortType’ in runtime, which can be either ‘id’ or ‘name’. I want to sort the list based on the value of the

Comparable and Comparator Interface in Java

房东的猫 提交于 2019-11-29 07:52:22
I want to write a generic Pair class, which has two members: key and value. The only requirement to this class is that both key and value should implements the Comparable interface, otherwise Pair class will not accept them as type parameter. First I code it like this: public class Pair<T1 extends Comparable, T2 extends Comparable> But the JDK 1.6 compiler will generate warning about this: Comparable is a raw type. References to generic type Comparable<T> should be parameterized Then I tried to add type parameters and the code now looks like this: public class Pair<T1 extends Comparable<?

Java Map sort by value

拥有回忆 提交于 2019-11-29 06:41:33
I was looking for ways of sorting Map<String, Integer> by values. I found this post , which solved my sorting problem, but not exactly. According to the post, I wrote the following code: import java.util.*; public class Sort { static class ValueComparator implements Comparator<String> { Map<String, Integer> base; ValueComparator(Map<String, Integer> base) { this.base = base; } @Override public int compare(String a, String b) { if (base.get(a) >= base.get(b)) { return 1; } else { return -1; } } } public static void main(String[] args) { HashMap<String, Integer> map = new HashMap<String, Integer

Java, Why it is implied that objects are equal if compareTo() returns 0?

孤者浪人 提交于 2019-11-29 05:34:00
Let's have a class Person . Person has a name and height. Equals and hashCode() takes into account only name. Person is comparable (or we implement comparator for it, does not matter which one). Persons are compared by height. It seems reasonable to expect a situation where two different persons can have same height, but eg. TreeSet behaves like comapareTo()==0 means equals, not merely same size. To avoid this, comparison can secondarily look at something else if size is the same, but then it cannot be used to detect same sized different objects. Example: import java.util.Comparator; import

Comparator and equals()

橙三吉。 提交于 2019-11-29 03:59:21
Suppose I need TreeSet with elements sorted with some domain logic. By this logic it doesn't matter order of some elements that doesn't equal so compare method can return 0, but in this case I couldn't put them in TreeSet . So, question: what disadvantages I'll have from code like this: class Foo implements Comparable<Foo>{} new TreeSet<Foo>(new Comparator<Foo>(){ @Override public int compare(Foo o1, Foo o2) { int res = o1.compareTo(o2); if(res == 0 || !o1.equals(o2)){ return o1.hashCode() - o2.hashCode(); } return res; } }); Update : Ok. If it should always be a consistency between the

How to use the Comparator interface

匆匆过客 提交于 2019-11-29 02:50:10
问题 I'm new to java, and I'm not really getting how to use the comparator interface. I have an ArrayList of Item s in an Inventory class and an Item class. In the Item class I wrote: public class Item implements Comparator<Item> { //stuff ... @Override public int compare(Item a, Item b) { if (a.getID().compareToIgnoreCase(b.getID())>0) return 1; else if (a.getID().compareToIgnoreCase(b.getID())<0) return -1; else return 0; } } The getID() method just gives the id, which I have to use to