How to sort these years in Descending Order in Java ?
Sample input :
1 1996 2 2015 3 2000
Expected output :
ID: 2 (2015)
ID: 3 (2000)
ID: 1 (1998)
Below is the code I have tried so fare:
import java.util.Collections;
import java.util.Scanner;
public class ArrayYear {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int[] arr = new int[3];
int[] year = new int[3];
// read element into array
for (int i = 0; i < arr.length; i++)
{
// input value
arr[i] = in.nextInt();
year[i] = in.nextInt();
}
// reverse array
for (int j = i + 1; j < year.length ; ++j)
{
if ( year[i] < year[j])
{
int temp = year[i];
year[i] = year[j];
year[j] = temp;
}
}
for (int i = 0; i < arr.length; i++)
{
// print array
System.out.println("ID :" + arr[i] + "(" + year[i] + ")");
}
}
}