View RSS Feed

Java Collection

Sorting Collections

Rate this Entry
by , 11-29-2011 at 06:23 PM (1202 Views)
If you are interested in sorting your collections, the good news is that since the inception of Java 5 it is now possible for collections and arrays to be sorted. This is very useful because it is a hell of a lot easier to find something in a sorted set than in an unsorted set. There are a number of sorting algorithms that can be used for your collections. There are listed below.
  • Bubble Sort algorithm
  • Bi-direction Bubble Sort algorithm
  • Quick Sort algorithm
  • Generic Sorting algorithm

It is a collection class that will allow for the sorting by doing repeated pair-wire comparisons using the sorting algorithms. Below is a simple example sorting an List of Strings alphabetically. Note that it is not the List that gives you a way to sort its contents, but the java.util.Collections class:

Java Code:
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class SortingExample {
	public static void main(String[] args){
	   Object[] data = {"Circle","Square","Rectangle",
	   List list = Arrays.asList(data);
	   System.out.println("unsorted " + list);
	   System.out.println("sorted " + list);

Submit "Sorting Collections" to Facebook Submit "Sorting Collections" to Digg Submit "Sorting Collections" to Submit "Sorting Collections" to StumbleUpon Submit "Sorting Collections" to Google

Tutorial , Sorting