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:

2464 questions
1207
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
  • 17,035
  • 16
  • 45
  • 83
199
votes
12 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,305
  • 2
  • 14
  • 15
178
votes
15 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
  • 7,343
  • 16
  • 47
  • 51
152
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
  • 73,989
  • 165
  • 410
  • 667
142
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
  • 108,809
  • 177
  • 454
  • 689
120
votes
12 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
  • 4,955
  • 9
  • 48
  • 84
120
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,117
  • 3
  • 18
  • 26
115
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
  • 1,989
  • 7
  • 20
  • 27
106
votes
9 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
  • 1,842
  • 5
  • 23
  • 33
90
votes
10 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,529
  • 4
  • 28
  • 52
88
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
  • 2,511
  • 3
  • 17
  • 32
86
votes
4 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,005
  • 7
  • 29
  • 50
86
votes
1 answer

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
  • 8,133
  • 5
  • 19
  • 38
72
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
  • 99,109
  • 82
  • 222
  • 258
60
votes
9 answers

comparator with null values

We have some code which sorts a list of addresses based on the distance between their coordinates. this is done through collections.sort with a custom comparator. However from time to time an address without coordinates is in the list causing a…
pvgoddijn
  • 11,653
  • 14
  • 43
  • 54
1
2 3
99 100