comparable

How to sort based on/compare multiple values in Kotlin?

浪子不回头ぞ 提交于 2019-11-26 08:06:54
问题 Say I have a class Foo(val a: String, val b: Int, val c: Date) and I want to sort a list of Foo s based on all three properties. How would I go about this? 回答1: Kotlin's stdlib offers a number of useful helper methods for this. First, you can define a comparator using the compareBy() method and pass it to the sortedWith() extension method to receive a sorted copy of the list: val list: List<Foo> = ... val sortedList = list.sortedWith(compareBy({ it.a }, { it.b }, { it.c })) Second, you can

Comparing the values of two generic Numbers

戏子无情 提交于 2019-11-26 06:42:53
问题 I want to compare to variables, both of type T extends Number . Now I want to know which of the two variables is greater than the other or equal. Unfortunately I don\'t know the exact type yet, I only know that it will be a subtype of java.lang.Number . How can I do that? EDIT : I tried another workaround using TreeSet s, which actually worked with natural ordering (of course it works, all subclasses of Number implement Comparable except for AtomicInteger and AtomicLong). Thus I\'ll lose

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 to implement the Java comparable interface?

一笑奈何 提交于 2019-11-26 01:34:05
问题 I am not sure how to implement a comparable interface into my abstract class. I have the following example code that I am using to try and get my head around it: public class Animal{ public String name; public int yearDiscovered; public String population; public Animal(String name, int yearDiscovered, String population){ this.name = name; this.yearDiscovered = yearDiscovered; this.population = population; } public String toString(){ String s = \"Animal name: \"+ name+\"\\nYear Discovered: \"

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

When should a class be Comparable and/or Comparator?

喜夏-厌秋 提交于 2019-11-25 23:36:12
问题 I have seen classes which implement both Comparable and Comparator . What does this mean? Why would I use one over the other? 回答1: The text below comes from Comparator vs Comparable Comparable A comparable object is capable of comparing itself with another object. The class itself must implements the java.lang.Comparable interface in order to be able to compare its instances. Comparator A comparator object is capable of comparing two different objects. The class is not comparing its instances