Home Tutorial Java Iterator Iterator Java Sort

 
 

Iterator Java Sort
Posted on: November 4, 2009 at 12:00 AM
In this section of tutorial we will learn how to use the static sort() method .We will create an example and sort the ArrayList.Then display the contents of the sorted ArrayList.

  • Sort is a static method of the Collections class.
  • It sorts the contents of the list Collection.


Example of Java Sort Iterator
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

public class sort {

	public static void main(String[] args) {
		List list = new ArrayList();
		String country[] = { "India", "Japan", "USA", "UK", "Nepal" };
		for (int i = 0; i < 5; i++) {
			list.add(country[i]);
		}
		Collections.sort(list);
		Iterator i = list.iterator();

		while (i.hasNext()) {
			System.out.print(i.next() + "\t");
		}
	}
}

Output

India Japan Nepal UK USA

Related Tags for Iterator Java Sort:


Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.