Thursday, 2 January 2020

Sort an array in ascending order


Following statement sorts the array in ascending order.
Arrays.sort(arr);

App.java
package com.sample.app;

import java.io.IOException;
import java.util.Arrays;

public class App {

	private static void printElements(int[] arr) {
		for (int i : arr) {
			System.out.println(i + " ");
		}
	}

	public static void main(String args[]) throws IOException {
		int[] arr = { 1, 3, 5, 7, 2, 4, 6, 8, 10 };
		Arrays.sort(arr);
		
		printElements(arr);

	}

}


Run App.java, you will see below messages in console.
1 
2 
3 
4 
5 
6 
7 
8 
10


You may like

No comments:

Post a Comment