Ordering Class | Guava | Java
Last Updated :
11 Jul, 2025
A
comparator, with additional methods to support common operations. This is an "enriched" version of Comparator. The common ways to get an instance of Ordering are :
- Subclass it and implement compare(T, T) instead of implementing Comparator directly.
- Pass a pre-existing Comparator instance to from(Comparator).
- Use the natural ordering, natural().
Declaration : The declaration for
com.google.common.collect.Ordering<T> class is :
@GwtCompatible
public abstract class Ordering<T>
extends Object
implements Comparator<T>
Below given are some methods provided by Guava's Ordering Class :
Ordering() : This is a constructor of Ordering Class of Guava. It constructs a new instance of this class (only invokable by the subclass constructor, typically implicit). Some other methods provided by this Class are :
Exceptions :
- explicit(List valuesInOrder) : NullPointerException if any of the provided values is null, IllegalArgumentException if valuesInOrder contains any duplicate values.
- explicit(T leastValue, T... remainingValuesInOrder) : NullPointerException if any of the provided values is null, IllegalArgumentException if any duplicate values.
- min(Iterator iterator) : NoSuchElementException if iterator is empty, ClassCastException if the parameters are not mutually comparable under this ordering.
- min(Iterable iterable) : NoSuchElementException if iterable is empty, ClassCastException if the parameters are not mutually comparable under this ordering.
- min(E a, E b) : ClassCastException if the parameters are not mutually comparable under this ordering.
- min(E a, E b, E c, E... rest) : ClassCastException if the parameters are not mutually comparable under this ordering.
- max(Iterator iterator) : NoSuchElementException if iterator is empty, ClassCastException if the parameters are not mutually comparable under this ordering.
- max(Iterable iterable) : NoSuchElementException if iterable is empty, ClassCastException if the parameters are not mutually comparable under this ordering.
- max(E a, E b) : ClassCastException if the parameters are not mutually comparable under this ordering.
- max(E a, E b, E c, E... rest) : ClassCastException if the parameters are not mutually comparable under this ordering.
- leastOf(Iterable iterable, int k): IllegalArgumentException if k is negative.
- leastOf(Iterator elements, int k) : IllegalArgumentException if k is negative.
- greatestOf(Iterable iterable, int k): IllegalArgumentException if k is negative.
- greatestOf(Iterator elements, int k) : IllegalArgumentException if k is negative.
- immutableSortedCopy : NullPointerException if any of elements (or elements itself) is null.
Some other methods provided by this class are :
Example 1 :
Java
// Java code to show implementation of
// Ordering class
import java.util.*;
import com.google.common.collect.Ordering;
class GFG {
// Driver code
public static void main(String args[])
{
// Creating a list of Integers
List<Integer> myList = new ArrayList<Integer>();
myList.add(new Integer(12));
myList.add(new Integer(3));
myList.add(new Integer(78));
myList.add(new Integer(50));
myList.add(new Integer(6));
myList.add(new Integer(70));
myList.add(new Integer(18));
myList.add(new Integer(9));
myList.add(new Integer(10));
// Displaying natural order of numbers
Ordering ordering = Ordering.natural();
System.out.println("Input List : " + myList);
// Displaying the sorted list
Collections.sort(myList, ordering);
System.out.println("Sorted List : " + myList);
}
}
Output :
Input List : [12, 3, 78, 50, 6, 70, 18, 9, 10]
Sorted List : [3, 6, 9, 10, 12, 18, 50, 70, 78]
Below given are some other methods provided by Ordering Class of Guava :
Example 2 :
Java
// Java code to show implementation of
// Ordering class
import java.util.*;
import com.google.common.collect.Ordering;
class GFG {
// Driver code
public static void main(String args[])
{
// Creating a list of Integers
List<Integer> myList = new ArrayList<Integer>();
myList.add(new Integer(12));
myList.add(new Integer(3));
myList.add(new Integer(78));
myList.add(new Integer(50));
myList.add(new Integer(6));
myList.add(new Integer(70));
myList.add(new Integer(18));
myList.add(new Integer(9));
myList.add(new Integer(10));
// Displaying natural order of numbers
Ordering ordering = Ordering.natural();
System.out.println("Minimum element is : " + ordering.min(myList));
}
}
Output :
Minimum element is : 3
Example 3 :
Java
// Java code to show implementation of
// Ordering class
import java.util.*;
import com.google.common.collect.Ordering;
class GFG {
// Driver code
public static void main(String args[])
{
// Creating a list of Integers
List<Integer> myList = new ArrayList<Integer>();
myList.add(new Integer(12));
myList.add(new Integer(3));
myList.add(new Integer(78));
myList.add(new Integer(50));
myList.add(new Integer(6));
myList.add(new Integer(70));
myList.add(new Integer(18));
myList.add(new Integer(9));
myList.add(new Integer(10));
// Displaying natural order of numbers
Ordering ordering = Ordering.natural();
System.out.println("Maximum element is : " + ordering.max(myList));
}
}
Output :
Maximum element is : 78
Example 4 :
Java
// Java code to show implementation of
// Ordering class
import java.util.*;
import com.google.common.collect.Ordering;
class GFG {
// Driver code
public static void main(String args[])
{
// Creating a list of Integers
List<Integer> myList = new ArrayList<Integer>();
myList.add(new Integer(12));
myList.add(new Integer(3));
myList.add(new Integer(78));
myList.add(new Integer(50));
myList.add(new Integer(6));
myList.add(new Integer(70));
myList.add(new Integer(18));
myList.add(new Integer(9));
myList.add(new Integer(10));
// Displaying natural order of numbers
Ordering ordering = Ordering.natural();
// To get reverse of original list
Collections.sort(myList, ordering.reverse());
// Displaying the reversed elements
System.out.println(myList);
}
}
Output :
[78, 70, 50, 18, 12, 10, 9, 6, 3]
Reference : Google Guava
Similar Reads
Longs Class | Guava | Java Longs is a utility class for primitive type long. It provides Static utility methods pertaining to long primitives, that are not already found in either Long or Arrays. Declaration : @GwtCompatible(emulated=true) public final class Longs extends Object Below table shows the Field summary for Guava L
3 min read
Range Class | Guava | Java Guavaâs Range represents an interval, for example, a < range < b. Here range includes any value between a and b, called endpoints which form the boundary. Any value between the boundary is a contiguous span of values of type Comparable. Declaration : The declaration for com.google.common.colle
5 min read
Joiner class | Guava | Java Guava's Joiner class provides various methods to handle joining operations on string, objects, etc. This class provides advanced functionality for the join operation. Declaration: Following is the declaration for com.google.common.base.Joiner class : @GwtCompatible public class Joiner extends Object
2 min read
Joiner class | Guava | Java Guava's Joiner class provides various methods to handle joining operations on string, objects, etc. This class provides advanced functionality for the join operation. Declaration: Following is the declaration for com.google.common.base.Joiner class : @GwtCompatible public class Joiner extends Object
2 min read
Shorts Class | Guava | Java Shorts is a utility class for primitive type short. It provides Static utility methods pertaining to short primitives, that are not already found in either Short or Arrays. Declaration : @GwtCompatible(emulated=true) public final class Shorts extends Object Below table shows the Field summary for Gu
3 min read
Splitter Class | Guava | Java Guava's Splitter Class provides various methods to handle splitting operations on string, objects, etc. It extracts non-overlapping substrings from an input string, typically by recognizing appearances of a separator sequence. This separator can be specified as a single character, fixed string, regu
2 min read