Can someone explain how this code work especially at the if statement. I dont have any idea on how it make the sorting, by comparing random variable and declaring int t and assign to variable num1, then num1 = num2 and after that num2 = t and so on. What does it mean actually, and how it sort the value according to the order?
package Lab4;
import javax.swing.JOptionPane;
public class test2 {
public static void main(String[] args) {
String stringNum1 = JOptionPane.showInputDialog(null,
"Please enter 1st Integer: ");
String stringNum2 = JOptionPane.showInputDialog(null,
"Please enter 2nd Integer: ");
String stringNum3 = JOptionPane.showInputDialog(null,
"Please enter 3rd Integer: ");
String stringNum4 = JOptionPane.showInputDialog(null,
"Please enter 4th Integer: ");
String stringNum5 = JOptionPane.showInputDialog(null,
"Please enter 5th Integer: ");
int num1 = Integer.parseInt(stringNum1);
int num2 = Integer.parseInt(stringNum2);
int num3 = Integer.parseInt(stringNum3);
int num4 = Integer.parseInt(stringNum4);
int num5 = Integer.parseInt(stringNum5);
if (num1 > num2) {
int t = num1;
num1 = num2;
num2 = t;
}
if (num4 > num5) {
int t = num4;
num4 = num5;
num5 = t;
}
if (num1 > num3) {
int t = num1;
num1 = num3;
num3 = t;
}
if (num2 > num3) {
int t = num2;
num2 = num3;
num3 = t;
}
if (num1 > num4) {
int t = num1;
num1 = num4;
num4 = t;
}
if (num3 > num4) {
int t = num3;
num3 = num4;
num4 = t;
}
if (num2 > num5) {
int t = num2;
num2 = num5;
num5 = t;
}
if (num2 > num3) {
int t = num2;
num2 = num3;
num3 = t;
}
if (num4 > num5) {
int t = num4;
num4 = num5;
num5 = t;
}
System.out.println("Ascending order : " + num1 + " " + num2 + " " + num3 + " " + num4 + " " + num5);
System.out.println("Discending order : " + num5 + " " + num4 + " " + num3 + " " + num2 + " " + num1);
}
}