2,802 questions
5
votes
0
answers
148
views
Why does the shuffle-found trigger order for a TimSort comparator contract violation *not* reliably reproduce the error when manually tested?
Background:
I'm debugging a Java sorting setup where I found a faulty Comparator already (violates the contract).
My goal is to implement a test case which provokes the well-known
java.lang....
0
votes
0
answers
118
views
ClassCastException when comparing elements in custom ArrayOrderedList using Comparator
I am implementing an ordered list that automatically inserts elements in the correct sorted position using a Comparator. The list is built from scratch (not using java.util.ArrayList) and uses an ...
2
votes
1
answer
67
views
How to sort a column by data from another column in JTable
I need to sort the column with "File creation date", but if the column "File type" == FyleType.DIRECTORY this row should be at the top.
That is, first sorting should be by type, ...
1
vote
2
answers
107
views
How do I use quicksort in C with a multi-dimensional array? I am getting an incorrect result
I am trying to sort the tuples in the 2D array based on a single column value similar to Javascript's sort function.
arr.sort((a,b) => a[0] - b[0])
C Code :
#include<stdio.h>
#...
-1
votes
3
answers
138
views
Java - What would be the better method for a compare method. - 2 examples [closed]
I have a few compare method in a class and im not sure which way i should do them
First option i came up
public static Comparator<Student> nameCompare(String str) {
if (str.equals("ASC&...
0
votes
0
answers
62
views
Generic binary search Java with Comparator and Comparable [duplicate]
I'm being asked to create a binary search that can use either Comparable or Comparator depending on which constructor is being used. I made a private boolean to note whether or not to use the object's ...
-2
votes
1
answer
168
views
Comparators in Java [duplicate]
In Java, how come that the comparators will take the values in pairs when we define the logic like this?
Integer[] arr = { 1, 2, 3, 4 };
Arrays.sort(arr, (a, b) -> { return a - b; });
How does it ...
4
votes
3
answers
266
views
How do you implement compareTo() in a class with a lot of fields when you only care about a single value?
In the case where we have a class with a lot of fields - let's say for example a BankAccount class:
class BankAccount implements Comparable<BankAccount> {
Double amount;
String name;
...
1
vote
2
answers
114
views
Compare two List of Objects in Java
I need some help with an issue I'm facing when comparing two lists of objects (TestFoo type). The goal is to check both list is equals or not
The Goal:
I need to compare existingItems and newItems (...
0
votes
2
answers
95
views
containsInAnyOrder hamcrest matcher with custom comparator
I have a DTO class
class MyDto {
private String f1;
private String f2;
private String f3;
// constructor
// get set equals/hashCode
}
It has equal/hascode pair which takes into ...
0
votes
0
answers
36
views
Non-static method cannot be referenced from a static contex error using Comparator [duplicate]
Even after reading documentations and multiple posts about this error I still don't understand the following case:
This line will generate the error Non-static method cannot be referenced from a ...
0
votes
0
answers
34
views
Cannot resolve method 'getTitle' in 'Object' [duplicate]
So, I'm kind of new to Java, and I was trying to test the reversed method ( in Comparator functional interface), and tried it in two different approaches. One with using it after defining a lambda ...
1
vote
1
answer
103
views
java comparator sort with model array and another field
I have a java class..
public class response_message {
private String message;
private String rejectCode;
private int ruleSeqNo;
private String contInd;
public String getMessage() ...
1
vote
3
answers
144
views
Sorting a list based on custom comparator while not changing the place of negative numbers
Sorting a list of integers except negative numbers in java , negative numbers indexes should remain same
I tried to achieve above condition using a custom comparator but it fails
list.sort((a,b)->(...
0
votes
1
answer
80
views
How to check if a comparator can be used on an object of unknown type?
Say I'm implementing a sorted set in Java with a generic <T>, like a binary search tree. The set uses a Comparator<T> to order its elements. For simplicity, let's assume the comparator is ...
3
votes
1
answer
110
views
How to sort descending with cpp 20 ranges projections
Say I have a Person class like this.
class Person
{
friend std::ostream &operator<<(std::ostream &out, const Person &operand);
public:
Person(std::string name, std::string ...
3
votes
1
answer
96
views
A pithy way to obtain the largest amongst a set of comparables
Context: Modern java (the latest version; JDK23 at time of writing this question)
Given 2 objects that are comparable (implement Comparable<T>, where T is a type they all share), how does one ...
7
votes
2
answers
215
views
Does it make any sense to define operator< as noexcept?
I know that it makes perfect sense to define e.g. move constructor as noexcept (if possible) and I think I understand the effect of that.
But I have never seen a similar discussion about operator<()...
0
votes
2
answers
82
views
Comparison in Kotlin
I'm a beginner in Kotlin and I'm trying to learn about comparison so i wrote this code:
fun main() {
val laptops = listOf(
laptop(2020, 8, 1000),
laptop(2022, 4, 800),
...
0
votes
1
answer
76
views
Why do we use the new operator to access the nested static class in Java?
The below is the Employee class which has a public static nested class called EmployeeNameComparator .
import java.util.Comparator;
public class Employee {
public static class ...
1
vote
1
answer
225
views
Why do I need to add 'const' in the custom comparator?
I am not very familiar with C++, and am confused about how a comparator works.
The below code is how I can find the position I want to insert a new interval:
#include <algorithm>
#include <...
0
votes
2
answers
47
views
Compare 4 columns, to find changes in QTY and return result in Google Sheets
Please I hope someone can help, I think is easy, but I am having problems to find the correct way for this online. I need to know changes in QTY to get a report to know what moves what does not.
1- ...
2
votes
1
answer
101
views
compareTo() method on integer (Java 8, Comparable, Comparator, sorting, String)
We can apply comparable & comparator both on integers, as like below:
List<Integer> intList1 = Arrays.asList(1,9,4,8,2,3,7,4,5);
Optional<Integer> val1 = intList1.stream().sorted((a, b)...
0
votes
1
answer
178
views
SQL Server less than or equal to (<=) date not including last date [duplicate]
I have a what seems to be a simple but odd finding. When I use:
WHERE DateTime >= '2024-04-01' AND DateTime <= '2024-04-30'
This above only gives me up to 04/29, but not 04/30
WHERE DateTime &...
0
votes
1
answer
58
views
Sorting keys in a dictionary with multiple keys while keeping certain keys together
I have a Dictionary with keys of type Setting, where setting is a struct with members mCategory (String), mIndex (int), and mDescription (String). Some of the keys are unique - there will be at most ...
0
votes
1
answer
137
views
VBA: If statement comparing cell addresses?
Writing a macro for data output and I need to compare cell addresses to ensure the macro is pulling data from the correct block of data.
Code Example:
Dim SectionSTART As Variant
SectionSTART = ...
2
votes
4
answers
129
views
thenComparing seems to not be lexicographic, despite the docs saying so
According to the docs,
thenComparing
default <U extends Comparable<? super U>> Comparator thenComparing(Function<? super T,? extends U> keyExtractor)
Returns a lexicographic-order ...
0
votes
4
answers
465
views
How to move a specific value to the end of the array without affecting the order of other elements?
Suppose I have an array of [0,7,4,0,0,8,3,0,0,0,5,6] and I have to take all the zeroes to last positions after other numbers without modifying their order.
So the output array should be like : [7,4,8,...
0
votes
1
answer
38
views
Python sorted comparator
I am trying to sort a list of tuples on the lexicographic order of the sub-string that the tuples are indicating.
In the following case, I wish to sort a which actaully represents ["sd", &...
1
vote
1
answer
119
views
Using a container inside a custom comparator for comparison
#include <set>
#include <string>
#include <unordered_map>
#include <vector>
int main()
{
std::vector<std::string> wordsContainer={"aba", "baba",&...
-1
votes
1
answer
66
views
How to implement equals method for class contains another class in java
I want to compare StudentDTO class object.
StudentDTO s1 = new StudentDTO();
StudentDTO s2 = new StudentDTO();
s1.equals(s2)
I have Class StudentDTO contains AddressDTO class object as ...
0
votes
1
answer
230
views
Sorting a TreeMap by object properties
I have a TreeMap full of Houses. Each house has different properties such as address, number of floors, etc. The TreeMap gets populated into a JList so that the user can select a house from the list, ...
-1
votes
1
answer
105
views
method sort(List<T>, Comparator<? super T>) in the type Collections is not applicable for arguments (ArrayList<Product>, CustomProductComparator)
Product with fields (id, productName, categoryName, etc).After making Arraylist of product and Populating it with Values like
ArrayList<Product> products = new ArrayList<Product>(Arrays....
-1
votes
1
answer
106
views
c++ using std::lower_bound to find element in array by 2 parameters
I have a problem using lower_bound to find element in array by 2 parameters.
I have a
struct person{
public:
person(const std::string CITY, const std::string ADDRESS, const std::string REGION, ...
1
vote
1
answer
60
views
Comparator on list with only null value
The following code return Nullpointer exception:
Comparator<LocalDate> c = (LocalDate d1,LocalDate d2)-> {return (d1.isAfter(d2) ? 1 : (d1.isBefore(d2)? -1: 0));} ;
List<LocalDate> ...
1
vote
2
answers
381
views
NullPointerException when using Comparator with nullsLast
I know there are many similar questions about the same issue. However, I've tried the solutions provided and none worked for me. I am 100% sure it is something basic I am missing, but I am not able to ...
0
votes
0
answers
203
views
Asking Question About Implementing stm32L431 Internal Comparator in Low Power Mode (Stop Mode)
I'm encountering an issue with the STM32 internal comparator (COMP). Although the signal comparison works as expected, I've observed that enabling the COMP causes a significant increase in current ...
-1
votes
2
answers
89
views
Custom Sort for multiple attributes In java
I have the below method for custom code to Sort and the requirement is that we have to do the custom sort based on BoardPoint, OffPoint, LastName, and FirstName.
Sort first based on BoardPoint
If ...
-2
votes
1
answer
99
views
How do I sort and rank the parent list by comparing the child list elements using java? [closed]
I need to find the Students ranks based on the subjectPriorityOrder given. Student list contains subject list with marks. Subject Priority Order list has the priority order which one we should have to ...
2
votes
1
answer
184
views
Does Delphi String-variant comparison use loInvariantLocale? How to locally enable loUserLocale instead?
My problem arises from DevExpress TcxGrid default comparison (used for the sorting of data), which boils down to the code (implemented in cxVariants.pas):
if VarIsEmpty(V1) then
if VarIsEmpty(V2)...
0
votes
1
answer
56
views
One list acts as a template to sort another list
Lets say there are two lists:
List<Integer> listA = Arrays.asList(1,2,3,4,5,6,7,6,5,4,3,2,1,0);
This list is a template for sorting another listB.
List<Integer> listB = Arrays.asList(0,106,...
0
votes
1
answer
117
views
Reference: Invariant for comparators in collections
Consider the following Java example:
int[] a = new int[]{1, 2};
PriorityQueue<Integer> pq = new PriorityQueue<Integer>(Comparator.comparingInt(i -> a[i]));
pq.add(0);
pq.add(1);
a[1] = ...
0
votes
1
answer
436
views
Java Comparison issue - Comparison method violates its general contract [duplicate]
I am trying to do a sorting of some number. I am getting a "java.lang.IllegalArgumentException: Comparison method violates its general contract!" Exception when I execute the following code.
...
0
votes
1
answer
160
views
Java Comparator, comparing two doubles:
I use Java 17. Here is my code:
public static class SortByPercentLike implements VideoSorterInterface {
// define Lambda Function
Comparator<Video> videoPercentLikeComparator ...
2
votes
1
answer
142
views
ExoPlayer TrackSelectionDialog comparator not working
I am printing video tracks to user in "height" + "p" format.
By default TrackSelectionDialog printing them let's say in random order. I want to reorder them from min to max.
So I ...
0
votes
1
answer
76
views
How to use a customized comparator to define the innermost map of a nested map in Java?
I have a class Range and a comparator in Java:
class Range{
int start;
int end;
}
class RangeComparator implements Comparator<Range> {
@Override
public int compare(Range range1, ...
-2
votes
2
answers
105
views
How to compare two different generic instantiations of a class in Java?
MRE because well, assignment... sigh
This is the problem I'm stuck on:
Consider the following class:
class MyClass<T extends Comparable<T>> implements Comparable<MyClass<T>> {
...
0
votes
1
answer
85
views
How do I sort a tree map using a comparator that uses both key and value
So I have this class where I'm trying to initialize a SortedMap using a Comparator I created in another class (separate files)
Why is this part not working?
Comparator<Map.Entry<Country, Pair<...
2
votes
5
answers
1k
views
Return the most recent date after comparing dates from array list in Java
How do I return the most recent date after comparing all the elements in the Array List? The expectation is to return 2023-01-03T02:00:00.800Z value as it is the most recent date in the list out of ...
0
votes
1
answer
379
views
Using Comparator.nullsFirst not working with reverse()
I am trying to sort Car by three values: first by type, then within the "type" bucket I want to sort those by price (lowest to highest), then if there are any that have the same price, I ...