Note: This document describes Clojure 1. A comparator is a function that takes two arguments x and y and returns a value indicating the relative order in which x and y should be sorted. It can be a 3-way comparator returning an integer, or a 2-way comparator returning a boolean. See the DOs below for what the return values should be, depending upon the order of x and y. In Clojure you need comparators for sorting a collection of values, or for maintaining a collection of values in a desired sorted order, e.
PriorityQueue comparator() Method in Java - GeeksforGeeks
The canonical reference for building a production grade API with Spring. If you have a few years of experience in the Java ecosystem, and you're interested in sharing that experience with the community and getting paid for your work of course , have a look at the "Write for Us" page. Cheers, Eugen. When working with custom types, or trying to compare objects that aren't directly comparable, we need to make use of a comparison strategy. We can build one simply, but making use of the Comparator or Comparable interfaces. Let's take an example of a football team — where we want to line up the players by their rankings. Next, let's create a PlayerSorter class to create our collection and make an attempt to sort it using Collections.
A comparator interface is used to order the objects of user-defined classes. A comparator object is capable of comparing two objects of two different classes. Method 1 : One obvious approach is to write our own sort function using one of the standard algorithms.