Home » Core Java » util » Comparator » Sort Vector using Comparator example

About 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.

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.add("element_1");
    vector.add("element_3");
    vector.add("element_5");
    vector.add("element_2");
    vector.add("element_4");
 
    // 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++)

System.out.println(vector.get(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
    Collections.sort(vector,comparator);

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

System.out.println(vector.get(i));
 
  }
}

Output:

Elements in Vector prior sorting :
element_1
element_3
element_5
element_2
element_4
Elements in Vector after sorting :
element_5
element_4
element_3
element_2
element_1

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

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

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

 

Subscribe
Notify of
guest

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

0 Comments
Inline Feedbacks
View all comments