How to sort Vector in Java – Ascending Descending Order Example

Sorting Vector in Java - Code Example


Sorting Vector is similar to Sorting any other Collection class in Java like ArrayList or HashSet. Only difference is that Vector is obsolete Collection and should not be used because support for Vector may be discontinued in newer Java releases. Nevertheless Vector is very popular collection and we often need to sort objects stored in Vector known as elements. Collections.sort() method is used to sort elements from Vector, this method is overloaded and accept an optional Comparator, if provided, compare() method of Comparator is used to compare objects otherwise compareTo() method of object is used to sort elements in natural order. In this Java program we will see how String stored in Vector will be sorted in ascending and descending order in String’s natural alphabetical order.

Java Code Example to Sort Vector in Java

Following Java program is a code example of sorting elements of Vector in Java on ascending and descending order. Collections.sort() method is used to sort String stored in Vector in lexicographical order. Since we are not providing any explicit comparator , compareTo() method of String is used to perform sorting or elements stored in Vector. If you are confused between natural order and any particular order of sorting than you can see Difference between Comparator and Comparable in Java. That will help us to understand what is natural order sorting.

import java.util.Collections;
import java.util.Vector;
/**
*
* Java program to sort Vector in Java on both ascending and descending order

* without using any third party library.
* By the way Vector is an obsolete class and consider using ArrayList

* or CopyOnWriteArrayList instead of Vector in Java.
*
*/
public class VectorSortingDemo {


public static void main(String args[]) {

//creating Vector of String to demonstrate Sorting in Java
//Remember Vector is an obsolete Collection
Vector<String> languages = new Vector<String>();
languages.add("English");
languages.add("Spanish");
languages.add("French");

System.out.println("unsorted Vector in Java : " + languages);


//Sorting Vector in Java on ascending order
Collections.sort(languages);
System.err.println("Sorted Vector in Java on ascending order : " + languages);

//Sorting Vector in descending order in Java
Collections.sort(languages, Collections.reverseOrder());
System.out.println("Vector sorted in Java on descending order : " + languages);

}
}

Output:
unsorted Vector in Java : [English, Spanish, French]
Sorted Vector in Java on ascending order : [English, French, Spanish]
Vector sorted in Java on descending order : [Spanish, French, English]

That's it on How to Sort Vector in Java on ascending and descending order. Elements stored in Vector must implement Comparable or a separate Comparator needs to be provided in order to sort them in ascending and descending order.

More by this Author


Comments

No comments yet.

    Sign in or sign up and post using a HubPages Network account.

    0 of 8192 characters used
    Post Comment

    No HTML is allowed in comments, but URLs will be hyperlinked. Comments are not for promoting your articles or other sites.


    Click to Rate This Article
    working