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.
  • Invoke reverseOrder() API method of Collections to get a Comparator that imposes the reverse of the natural ordering on the vector’s elements.
  • Invoke sort(List list, Comparator c) API method of Collections in order to sort the vector elements using the comparator. The vector’s elements will be sorted according to the comparator.
  • We can get the vector’s elements before and after sorting to check how they are sorted. Before sorting the vector elements are sorted by insertion order,

as described in the code snippet below.

package com.javacodegeeks.snippets.core;
import java.util.Vector;
import java.util.Collections;
import java.util.Comparator;
public class SortArrayListComparator {
  public static void main(String[] args) {
    // Create a Vector and populate it with elements
    Vector vector = new Vector();
    // Vector implementation maintains the insertion order for its elements
    System.out.println("Elements in Vector prior sorting :");
    for(int i=0; i < vector.size(); i++)



To get a comparator that imposes reverse order on a Collection's elements 

we can use static Comparator reverseOrder() operation of Collections class
    Comparator comparator = Collections.reverseOrder();
    // Using Collection.sort(List list, Comparator c) static operation we can sort Vector elements using a Comparator

    System.out.println("Elements in Vector after sorting :");
    for(int i=0; i < vector.size(); i++)



Elements in Vector prior sorting :
Elements in Vector after sorting :

This was an example of how to sort a Vector using a Comparator in Java.

Want to know how to develop your skillset to become a Java Rockstar?

Join our newsletter to start rocking!

To get you started we give you our best selling eBooks for FREE!


1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design


and many more ....


Receive Java & Developer job alerts in your Area

I have read and agree to the terms & conditions


Byron Kiourtzoglou

Byron is a master software engineer working in the IT and Telecom domains. He is an applications developer in a wide variety of applications/services. He is currently acting as the team leader and technical architect for a proprietary service creation and integration platform for both the IT and Telecom industries in addition to a in-house big data real-time analytics solution. He is always fascinated by SOA, middleware services and mobile development. Byron is co-founder and Executive Editor at Java Code Geeks.
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Inline Feedbacks
View all comments
Back to top button