Comparator

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.

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.
Subscribe
Notify of
guest

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

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button