Sorting and Comparing Three integers in Java

In this tutorial, we will compare 3 integers and sort them in ascending order. To do so, we will use user inputs as our integers.

Sorting and Comparing Three integers in Java

Let's compare and sort three integers

Note: numbers are assumed to differ from each other

``````Scanner input = new Scanner(System.in);
System.out.println("What is the first integer?");
int a=input.nextInt();

System.out.println("What is the second integer?");
int b=input.nextInt();

System.out.println("What is the third integer?");
int c=input.nextInt();

if (a >= b){
if (b >= c)
System.out.print("Order of three numbers: " + c + " "+ b + " " + a);

else if  (c >= a)
System.out.print("Order of three numbers: " + b + " "+ a + " " + c);

else if (a > c)
System.out.print("Order of three numbers: " + b + " " + c + " " + a);
}

if (b > a){
if (c >= b)
System.out.print("Order of three numbers: " + a + " " + b + " "+ c);
else if (c >= a)
System.out.print("Order of three numbers: " + a + " " + c + " " + b);
else if (a > c)
System.out.print("Order of three numbers: " + c + " " + a + " " + b);
}``````

Output:

What is the first integer?
7
What is the second integer?
8
What is the third integer?
6
Order of three numbers: 6 7 8