Questions tagged [comparator]

A Common interface to create an object that specifies the way of comparing other objects. When using this tag on implementation heavy questions - tag the code language the implementation is written in.

A Common interface to create an object that specifies the way of comparing other objects.

Generally, the sorting algorithm takes two parameters - two objects which should be compared. The comparison is then performed between the two objects:

  • A negative return value indicating the first object is smaller than the second in the sorted order.
  • A zero return value indicating the two objects are equal in the sorted order.
  • A positive value indicating the first object is larger than the second in the sorted order.

Java and other languages define the ability to separate sorting algorithms from the specific comparison used.

In Java, for instance, one of the mechanisms is to define an object that implements the Comparator interface. This object can then be used as an argument when calling sorting methods.

See also:

2761 questions
1279
votes
29 answers

Sort ArrayList of custom Objects by property

I read about sorting ArrayLists using a Comparator but in all of the examples people used compareTo which according to some research is a method for Strings. I wanted to sort an ArrayList of custom objects by one of their properties: a Date…
Samuel
  • 18,286
  • 18
  • 52
  • 88
221
votes
14 answers

"Comparison method violates its general contract!"

Can someone explain me in simple terms, why does this code throw an exception, "Comparison method violates its general contract!", and how do I fix it? private int compareParents(Foo s1, Foo s2) { if (s1.getParent() == s2) return -1; if…
n00bster
  • 2,525
  • 2
  • 16
  • 15
200
votes
16 answers

How to use Comparator in Java to sort

I learned how to use the comparable but I'm having difficulty with the Comparator. I am having a error in my code: Exception in thread "main" java.lang.ClassCastException: New.People cannot be cast to java.lang.Comparable at…
Dan
  • 8,263
  • 16
  • 51
  • 53
159
votes
2 answers

Java : Comparable vs Comparator

Possible Duplicates: difference between compare() and compareTo() Java: What is the difference between implementing Comparable and Comparator? What are the keys differences between Comparable and Comparator. and which is preferred over the…
daydreamer
  • 87,243
  • 191
  • 450
  • 722
149
votes
4 answers

Comparator.reversed() does not compile using lambda

I have a list with some User objects and i'm trying to sort the list, but only works using method reference, with lambda expression the compiler gives an error: List userList = Arrays.asList(u1, u2, u3); userList.sort(Comparator.comparing(u ->…
Andrey
  • 2,485
  • 3
  • 21
  • 26
146
votes
11 answers

When should a class be Comparable and/or Comparator?

I have seen classes which implement both Comparable and Comparator. What does this mean? Why would I use one over the other?
Nick Heiner
  • 119,074
  • 188
  • 476
  • 699
142
votes
6 answers

Reverse a comparator in Java 8

I have an ArrayList and want sort it in descending order. I use for it java.util.stream.Stream.sorted(Comparator) method. Here is a description according Java API: Returns a stream consisting of the elements of this stream, sorted according to the…
Guforu
  • 3,835
  • 8
  • 33
  • 52
136
votes
8 answers

How does Javascript's sort() work?

How does the following code sort this array to be in numerical order? var array=[25, 8, 7, 41] array.sort(function(a,b){ return a - b }) I know that if the result of the computation is... Less than 0: "a" is sorted to be a lower index than…
cw84
  • 2,111
  • 5
  • 24
  • 37
133
votes
15 answers

Android-java- How to sort a list of objects by a certain value within the object

Im trying to sort through an arraylist of objects by a particular value within the object. What would be the best approach to do such a thing. Should I use Collections.sort() with some kind of comparator? Im trying to sort a list of objects by a…
James andresakis
  • 5,335
  • 9
  • 53
  • 88
123
votes
19 answers

When to use Comparable and Comparator

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…
pkrish
  • 2,229
  • 7
  • 22
  • 29
121
votes
13 answers

Java error: Comparison method violates its general contract

I saw many questions about this, and tried to solve the problem, but after one hour of googling and a lots of trial & error, I still can't fix it. I hope some of you catch the problem. This is what I get: java.lang.IllegalArgumentException:…
Lakatos Gyula
  • 3,949
  • 7
  • 35
  • 56
110
votes
2 answers

null-safe mapping Comparator using default implementations

Is there a build-in possibility to create a null-safe mapping comparator in Java 8 without writing a own implementation of Comparator? When running the following code, it causes a NPE because the keyExtractor argument of Comparator.comparing() may…
flo
  • 9,713
  • 6
  • 25
  • 41
106
votes
4 answers

Very confused by Java 8 Comparator type inference

I've been looking at the difference between Collections.sort and list.sort, specifically regarding using the Comparator static methods and whether param types are required in the lambda expressions. Before we start, I know I could use method…
Tranquility
  • 3,061
  • 5
  • 23
  • 37
80
votes
8 answers

Natural sort order string comparison in Java - is one built in?

I'd like some kind of string comparison function that preserves natural sort order1. Is there anything like this built into Java? I can't find anything in the String class, and the Comparator class only knows of two implementations. I can roll my…
Kip
  • 107,154
  • 87
  • 232
  • 265
64
votes
10 answers

Comparator with double type

I have written the following code: public class NewClass2 implements Comparator { public int compare(Point p1, Point p2) { return (int)(p1.getY() - p2.getY()); } } If I let's say have two double numbers, 3.2 - 3.1, the…
user472221
  • 3,014
  • 11
  • 35
  • 42
1
2 3
99 100