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] + ")");
}
}
}
Try should work for array reversing
for (int i = arr.length; i = 0; i--)
{
// print array
System.out.println("ID :" + arr[i] + "(" + year[i] + ")");
}
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 year[i] in descending order
for (int j = 0 ; j < year.length ; j++)
{
if ( year[i] < year[j] )
{
int temp = year[i];
year[i] = year[j];
year[j] = temp;
}
}
}
for (int i = arr.length-1 ; i >= 0 ; i--)
{
// print array
System.out.println("ID :" + arr[i] + "(" + year[i] + ")");
}
}
}
//my output :
ID :3(2001)
ID :2(2000)
ID :1(1988)
//sample output:
ID: 2 (2001)
ID: 3 (2000)
ID: 1 (1998)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.