I have a comparator of type Comparator<Integer> and a function Function<Pair<Integer,?>,Integer> expressed as Pair::left (that returns an Integer). I need to obtain a comparator of type Comparator<Pair<Integer,?>>. If I wanted to simply map a function Function<T,U> to a resulting function Function<T,V> though a function Function<U,V> I could simply apply andThen() method like this: Is it possible to obtain Comparator<Pair<Integer,?>> in
Tag: comparator
Create a SortedMap from a List objects with the Value represented as a list of N lowest object’s attributes mapped to a particular Key
I am working with a CSV file which includes some information about accidents. I’ve created the Accident type: I’ve also created a function that reads all the CSV file, converts all the accidents to a List<Accident> and saves it to this type AccidentArchive: So, we are working with streams which I don’t understand entirely yet, and I’ve been stuck in
Sorting a List of Maps dynamically in Ascending and Descending order
I am having Maps inside a List. I need to sort the List based on the input dynamically by passing it as a parameter into the method sortData. It is working as expected, but the problem is that am not able to sort the list in reverse order. I’m getting an Error : The method get(String) is undefined for the
Java sorting list of array vs sorting list of list
I have a list of points where each point is a tiny list of size 2. I want to sort the list of points in increasing order of x and if x values are equal, I break tie by sorting in decreasing order of y. I wrote a custom comparator to sort the points like this: Here’s the input before
Can’t pass wildcard Comparator to function
I have the following sort method in a clone of ArrayList: The sorter object is an instance of Sort: I get the following error on the sorter.mergeSort line: I’m not sure why this is happening as both the parameter and argument have type Comparator<? super E>. Answer Thanks to everyone for their comments. I’ve now fixed it. As Kayaman said,
How to Sort List based on Inner Object’s property in java 8
I have classes structure similar to: I have two different tables Source and Target, and after joining them I am getting four columns in the query output. I am constructing Response object using values of these four columns. I have data in Response object with these 4 properties sourceId, sourceName, targetId, targetName. I can have sourceId, sourceName same on the
Sort character array using java comparator
Sort a character array lexicographically with an additional condition that all c’s comes before all b’s. This could be done manually but I want to code it via inbuilt sorting using comparators. The code I wrote – Gives output: [a, a, a, a, a, b, c, b, d, r, r]. The c only comes before one of the b’s. If
arranging the records in customised way
Sort Procedure First sort by the delivered date & in case of multiple records on same date sort by delivered time (Note: if a record has both ordered and Delivered Date ,it should be sorted by Delivered date only) 08-JUN: 03.00, 08-JUN: 04.00 Then look at the ordered date without delivered date which also exists for 08-JUN & sort by
How to create a type specific Comparator for a flexible type array?
I am stuck in the middle of ‘generic-type-qualifier-land’ and can’t find the right combination of type qualifiers. I have a class that represents a flexible array that can have a number of different types in it. The class holds an ArrayList<Object> as the container for the elements. There are type specific accessors, like getString(i) and getInt(i) etc. I would like
Comparator.comparing(…).thenComparing(…) find out which fields did not match
I am trying to compare two objects of same class and the goal is to compare them as well as identify which fields didn’t match. Example of my domain class @Builder(toBuilder=true) class Employee { …