Home » Core Java » util » Comparator


org.apache.commons.io.comparator.DefaultFileComparator Example

Apache Commons IO is a library of utilities to assist with developing IO functionality. org.apache.commons.io.comparator package provides various Comparator implementations for Files. DefaultFileComparator is one of the classes. This class can be used to sort lists or arrays of files (File objects) by using the default file comparison. This article shows usage of this class. The class is from Apache ...

Read More »

Comparator Java Example

In this post, we feature a comprehensive Java comparator Example. We will show how to use java.util.Comparator interface. java.util.Comparator is an interface which is used for sorting objects in Java. The compare(Object o1, Object o2) method of Comparator interface needs to be implemented for this purpose, which compares two objects and returns an integer, depending on the comparison: positive, if ...

Read More »

Java Comparable and Comparator Example to sort Objects

In Java, it’s very easy to sort an array or a list with primitive types. But you can also use Comparableand Comparator interfaces when you want to be able to short arrays or lists of your own custom objects. Let’s begin with a very simple example using arrays of primitive types: ObjectSortingExample.java: package com.javacodegeeks.java.core; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; ...

Read More »

Sort Vector using Comparator example

In this example we shall show you how to sort a Vector using a Comparator. The Collections API provides methods to get a Comparator and use it to sort a Vector. To sort a Vector using a Comparator one should perform the following steps: Create a new Vector. Populate the vector with elements, using add(Object o) API method of Vector. ...

Read More »

Sort ArrayList using Comparator example

With this example we are going to demonstrate how to sort an ArrayList using a Comparator. We are using Collections API, that provides methods to get and use a Comparator. In short, to sort an ArrayList using a Comparator you should: Create a new ArrayList. Populate the arrayList with elements, using add(E e) API method of ArrayList. Invoke reverseOrder() API ...

Read More »