How to sort an Array?

public class ArraySorting {
	public static void main(String[] args) {
		String[] animals = new String[] { "Dog", "Elephant", "Cat", "Tiger" };

		Arrays.sort(animals);

		int i = 0;
		for (String animal : animals) {
			System.out.println("Animal " + ++i + " : " + animal);
		}
	}
}
Output
Animal 1 : Cat Animal 2 : Dog Animal 3 : Elephant Animal 4 : Tiger


















Receive Latest Java Examples in Your Email: