comparator

Nice general way to sort nulls to the bottom, regardless?

╄→尐↘猪︶ㄣ 提交于 2019-12-18 16:31:55
问题 I'm writing some custom Comparators, and I'd like them to push null items to the bottom of the list, regardless of whether I'm sorting ascending or descending. What's a good strategy or pattern for approaching this? Offhand: Simply write separate ascending and descending comparators, sharing code where possible Delegate null handling to another class, either by throwing an NPE or by calling it explicitly Include an ascending flag and put conditional logic in it to navigate around the nulls

Nice general way to sort nulls to the bottom, regardless?

拈花ヽ惹草 提交于 2019-12-18 16:31:22
问题 I'm writing some custom Comparators, and I'd like them to push null items to the bottom of the list, regardless of whether I'm sorting ascending or descending. What's a good strategy or pattern for approaching this? Offhand: Simply write separate ascending and descending comparators, sharing code where possible Delegate null handling to another class, either by throwing an NPE or by calling it explicitly Include an ascending flag and put conditional logic in it to navigate around the nulls

Jasper Reports crosstab sorting with comparatorExpression

半腔热情 提交于 2019-12-18 11:33:08
问题 I'm trying to sort my dynamic columns in a cross tab according to some custom scheme. In the docs I found mention of comparatorExpression: Crosstab group bucket comparator expression. The result of this expression is used to sort the buckets, in ascending or descending order. If no comparator expression is specified, the natural order will be used. but I don't understand what the expression should look like. Can I somehow use a regular java comparator? Can someone share an example? 回答1: I've

How to pass a custom comparator to “sort”?

旧巷老猫 提交于 2019-12-18 11:03:24
问题 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! ? 回答1: 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

Trouble setting a column comparator to a JTable

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-18 09:25:23
问题 In another thread I found this comparator (bottom of post) for sorting JTable columns that could be composed of integers, strings, or both. I cannot figure out how to apply it to my JTable. My table was using the auto created row sorter before. I set that to false and I am now using: TableRowSorter<MyTableModel> rowSorter = new TableRowSorter<MyTableModel>(); jtable.setRowSorter(rowSorter); rowSorter.setComparator(0, c1); I get an index out of bounds exception saying I am providing an invalid

Why Comparator.comparing doesn't work with String::toLowerCase method reference?

泪湿孤枕 提交于 2019-12-18 05:47:05
问题 I am trying to sort an array of Strings by reverse order (ignoring case), without modifying it, and just printing it. So I am using Java8 stream. But I can't manage to do it. Here is my attempt : package experimentations.chapter02; import java.util.Arrays; import java.util.Comparator; import java.util.stream.Collectors; public class StringStream { public static void main(String[] args) { sortStrings(); } public static void sortStrings(){ String[] stringsArray = "The quick brown fox has a

Comparable and Comparator Interface in Java

一个人想着一个人 提交于 2019-12-18 05:07:07
问题 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

Java Map sort by value

六眼飞鱼酱① 提交于 2019-12-18 04:46:12
问题 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; } }

How can Comparator be a Functional Interface when it has two abstract methods? [duplicate]

大兔子大兔子 提交于 2019-12-18 03:50:34
问题 This question already has answers here : Precise definition of “functional interface” in Java 8 (6 answers) Closed 2 years ago . In Java 8, the @FunctionalInterface annotation is introduced to denote any interface that has exactly one abstract method as a functional interface. One of the reason for its introduction is to indicate the user (programmer), that lambda expression can be used in context of a functional interface. The Comparator interface is annotated with @FunctionalInterface . But

How to use Java comparator properly?

坚强是说给别人听的谎言 提交于 2019-12-17 20:36:16
问题 If I have the following class: public class Employee { private int empId; private String name; private int age; public Employee(int empId, String name, int age) { // set values on attributes } // getters & setters } How can I use comparator that compares by name, then age, then id? 回答1: You need to implement it so that it orders by preferred elements. That is, you need to compare by name, then if that comparison is equal, compare by age, etc. An example is listed below: public class