comparator

How do I sort enum members alphabetically in Java?

杀马特。学长 韩版系。学妹 提交于 2019-11-30 12:37:33
I have an enum class like the following: public enum Letter { OMEGA_LETTER("Omega"), GAMMA_LETTER("Gamma"), BETA_LETTER("Beta"), ALPHA_LETTER("Alpha"), private final String description; Letter() { description = toString(); } Letter(String description) { this.description = description; } public String getDescription() { return description; } } Later down my code I basically iterate over the Letter enum and print its members out to the console: for (Letter letter : Letter.values()) { System.out.println(letter.getDescription()); } I thought that the values() method would give me an ordered view

Java comparator: Two ordering criteria

流过昼夜 提交于 2019-11-30 09:55:50
问题 I have a simple class that contains a string (name) and an integer (age). The objects, that shall be stored in the collection, must not have double name values and shall be sorted according to descending age. The first code example removes all double names, but doesn't contain a second ordering criterion: public int compare(Person p1, Person p2) { int reVal = 1; if(p1.getName().compareTo(p2.getName()) != 0){ reVal = 1; } else { reVal = 0; } return reVal; } The next example comparator shall

sort a list of objects based on runtime property

夙愿已清 提交于 2019-11-30 07:29:42
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 string. I have tried to use comparator and reflection together, but no luck. May be I didn't use it

Comparable 和 Comparator 的区别

雨燕双飞 提交于 2019-11-30 07:16:07
Java 中为我们提供了两种比较机制:Comparable 和 Comparator,他们之间有什么区别呢?今天来了解一下。 Comparable 自然排序 Comparable 在 java.lang 包下,是一个接口,内部只有一个方法 compareTo(): public interface Comparable<T> { public int compareTo(T o); } Comparable 可以让实现它的类的对象进行比较,具体的比较规则是按照 compareTo 方法中的规则进行。这种顺序称为 自然顺序。 compareTo 方法的返回值有三种情况: e1.compareTo(e2) > 0 即 e1 > e2 e1.compareTo(e2) = 0 即 e1 = e2 e1.compareTo(e2) < 0 即 e1 < e2 注意: 1.由于 null 不是一个类,也不是一个对象,因此在重写 compareTo 方法时应该注意 e.compareTo(null) 的情况,即使 e.equals(null) 返回 false,compareTo 方法也应该主动抛出一个空指针异常 NullPointerException。 2.Comparable 实现类重写 compareTo 方法时一般要求 e1.compareTo(e2) == 0 的结果要和 e1

How to use the Comparator interface

无人久伴 提交于 2019-11-30 05:04:06
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 alphabetize the items. I'm not sure if this is right, it made me put the @Override annotation, I'm not sure why.

How to pass a custom comparator to “sort”?

≡放荡痞女 提交于 2019-11-30 01:34:46
Class A has the following comparator: class A attr_accessor x def my_comparator(a) x**2 <=> (a.x)**2 end end I would like to use this comparator to sort an array where each item is of class A: class B def my_method items.sort!(<how can I pass my_comparator here ?>) end end How should I pass my_comparator to sort! ? Define your own <=> , and include Comparable. This is from the Comparable doc : class SizeMatters include Comparable attr :str def <=>(an_other) str.size <=> an_other.str.size end def initialize(str) @str = str end def inspect @str end end s1 = SizeMatters.new("Z") s2 = SizeMatters

Does a natural comparator exist in the standard api?

孤街浪徒 提交于 2019-11-30 01:10:09
I need a comparator as part of a strategy pattern that can either use the natural ordering of the objects or some custom ordering. For the natural ordering case, I wrote a simple comparator: private static class NaturalComparator<T extends Comparable<? super T>> implements Comparator<T> { @Override public int compare(T o1, T o2) { return o1.compareTo(o2); } } Seems simple enough, but I was wondering if anyone knew of one in the standard API. I looked at TreeMap, and it does it without such a class, so when that code was written, the apparent answer would be no, but perhaps it was added later.

Java comparator for multi-column sorting?

久未见 提交于 2019-11-29 18:54:37
问题 Is there any Java open-source comparator for comparing beans by multiple fields for multi-column sorting? Each column can be sorted asceding or descending. For single-column sorting it can be achieved by using org.apache.commons.beanutils.BeanComparator together with org.springframework.util.comparator.InvertibleComparator . I'm aware that this functionality is quite trivial to write, but what's the benefit from reinventing the wheel, if it was already written and tested? 回答1: I wrote this a

Java comparator: Two ordering criteria

点点圈 提交于 2019-11-29 17:47:51
I have a simple class that contains a string (name) and an integer (age). The objects, that shall be stored in the collection, must not have double name values and shall be sorted according to descending age. The first code example removes all double names, but doesn't contain a second ordering criterion: public int compare(Person p1, Person p2) { int reVal = 1; if(p1.getName().compareTo(p2.getName()) != 0){ reVal = 1; } else { reVal = 0; } return reVal; } The next example comparator shall order the rest set of the objects, that doesn't contain any double names: public int compare(Person p1,

How do I sort enum members alphabetically in Java?

陌路散爱 提交于 2019-11-29 17:41:13
问题 I have an enum class like the following: public enum Letter { OMEGA_LETTER("Omega"), GAMMA_LETTER("Gamma"), BETA_LETTER("Beta"), ALPHA_LETTER("Alpha"), private final String description; Letter() { description = toString(); } Letter(String description) { this.description = description; } public String getDescription() { return description; } } Later down my code I basically iterate over the Letter enum and print its members out to the console: for (Letter letter : Letter.values()) { System.out