Click here to Skip to main content
16,019,273 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How to display items of array in increasing order of Array in java?

input
2
5
4
7

output
2
4
5
7

What I have tried:

/* package codechef; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
// System.out.println("enter the no of number you want to enter");
Scanner s=new Scanner(System.in);
int a=s.nextInt();
int digits[]=new int[a];
for(int i=0;i<a;i++)
{
Scanner number=new Scanner(System.in);
int inputnumber=number.nextInt();
digits[i]=inputnumber;
}
int max;
max=digits[0];
public int[] sort()
{
for( i=0;i<a;j++)
{
for(j=0;i<a-i;j++)
{
if(digits[j]>digits[j+1])
{
temp=digits[j];
digits[j]=digits[j+1];
temp[j+1]=temp;
}

}


}
return digits;
}
Codechef obj=new Codechef();
obj.sort();

}
}
Posted
Updated 12-Jan-18 9:55am
Comments
[no name] 13-May-17 18:06pm    
Just like you are doing?
Patrice T 13-May-17 18:18pm    
And you have a question ? a problem ?
Patrice T 13-May-17 19:16pm    
What is the question ? problem ?

Arrays class contains a method named sort for sorting an array,here is a simple example

Java
import java.util.Arrays;
class ArraysExample1
{
	public static void main(String args[])
	{
		int arr[] = {5,4,3,2,1};
		Arrays.sort(arr);
		for(int value:arr) { System.out.print(value+", ");}
	}
}


This will produce result as 1, 2, 3, 4, 5,

brief read here: Arrays (Java Platform SE 7 )[^]
 
Share this answer
 
v2
Use the line Arrays.sort(arrayname);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900