0

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] + ")");
    }
  }
}
5
  • You would need to sort the array in reverse order before printing it, right? Commented Oct 26, 2015 at 21:48
  • 1
    Possible duplicate of Sorting an array of Strings with Java Commented Oct 26, 2015 at 21:49
  • please search for a solution before posting a question Commented Oct 26, 2015 at 21:54
  • yes .. ok i'll try . thnk u :) Commented Oct 26, 2015 at 21:55
  • You may need to call Collections.sort(list, Collections.reverseOrder()); Commented Oct 26, 2015 at 22:55

2 Answers 2

1

Try should work for array reversing

for (int i = arr.length; i = 0; i--)
  {
   // print array
   System.out.println("ID :" + arr[i] + "(" + year[i] + ")");
  }
Sign up to request clarification or add additional context in comments.

2 Comments

should be "for (int i = arr.length-1; i >= 0; i--)"
Yes @ Eddie... Should have given it a second thought
0
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)

1 Comment

how to make the first array element hold the second array element ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.