I have this code which sorts any given number to ascending order, but I'm having difficulties figuring out how to sort the numbers given into a descending order.

Code:

import javax.swing.*;

import java.util.Arrays;

public class sort7

{
	public static void main(int[]array)
	//public static void main(String[]args)
	{
		String str1="";
		String str2="";
		String str3="";
		String str4="";
		String str5="";

		int num1=0;
		int num2=0;
		int num3=0;
		int num4=0;
		int num5=0;

		str1=JOptionPane.showInputDialog("Enter first number");
		num1=Integer.parseInt(str1);
		
		str2=JOptionPane.showInputDialog("Enter second number");
		num2=Integer.parseInt(str2);

		str3=JOptionPane.showInputDialog("Enter third number");
		num3=Integer.parseInt(str3);
		
		str4=JOptionPane.showInputDialog("Enter fourth number");
		num4=Integer.parseInt(str4);

		str5=JOptionPane.showInputDialog("Enter last number");
		num5=Integer.parseInt(str5);

		System.out.println("Your have entered:" +num1 +", " +num2 +", " +num3 +", " +num4 +" and " 

+num5 +"."  );

		
		double[] lengths = {num1, num2, num3, num4, num5};
		//double[] lengthsb = {num5, num4, num3, num2, num1};

        	Arrays.sort(lengths);
        	//Arrays.sort(lengthsb);
	Arrays.sort(lengths, collections.reverseOrder());

        	System.out.println("Ascending order:" + Arrays.toString(lengths));
        	//System.out.println("Descending order:"+ Arrays.toString(lengthsb));
			
	


	}
	
}

I have also tried the array.collecitonsRevers, but I seem to not get it right.

Any help would be much appreciated. Thanks!

Try putting your numbers inside an array and do

Arrays.sort(arrayName)

this will sort the numbers from lowest to highest.

to arrange it from highest to lowest, you need to do a for loop beginning from arrayName.length() - 1 to 0.

Edited 5 Years Ago by Eric Cute: n/a

Hi Eric,

Thanks for your quick reply. I really appreciate your help. I'm a back to school guy and my last programming subjects were 4 year ago, so, practically, I have forgotten the loops, would you mind expressing the actual coding if that's not too much to ask?Thanks!

Sure.

the for loop structure is like this

int i, j;

for (i = 0; i < j; i++) {
// your codes here
}

i is your counter
j is the max counter
i++ is the increment which means you add 1 to i after each iteration.

In your case, you should do the reverse so

i will be your arrayLength() -1
j will be 0
i-- should be the increment

Hope this helps (",)

Thanks!

Here's what I did, but I'm still having compile time errors:

import javax.swing.*;

import java.util.Arrays;

public class sort7c

{

	public static void main(String[]array)
	{
		int i, j;
 
		for (i = 0; i < j; i++)
		{		

			String str1="";
			String str2="";
			String str3="";
			String str4="";
			String str5="";
	
			int num1=0;
			int num2=0;
			int num3=0;
			int num4=0;
			int num5=0;
	
			str1=JOptionPane.showInputDialog("Enter first number");
			num1=Integer.parseInt(str1);
			
			str2=JOptionPane.showInputDialog("Enter second number");
			num2=Integer.parseInt(str2);
	
			str3=JOptionPane.showInputDialog("Enter third number");
			num3=Integer.parseInt(str3);
			
			str4=JOptionPane.showInputDialog("Enter fourth number");
			num4=Integer.parseInt(str4);
	
			str5=JOptionPane.showInputDialog("Enter last number");
			num5=Integer.parseInt(str5);
	
			System.out.println("Your have entered:" +num1 +", " +num2 +", " +num3 +", " +num4 +" and " +num5 +"."  );

		
			//double[] lengths = {num1, num2, num3, num4, num5};
			double[] lengthsb = {num5, num4, num3, num2, num1};
	
		        	//Arrays.sort(lengths);
	        		Arrays.sort(lengthsb);
	
       			//System.out.println("Ascending order:" + Arrays.toString(lengths));
	        		System.out.println("Descending order:"+ Arrays.toString(lengthsb));
			
		}
	}
	
}

Can you make the necessary corrections in the actual coding I used, so that after inputting 5 numbers, it will print both ascending and descending orders of the numbers? Thanks a lot again dude!

Edited 5 Years Ago by __avd: Added [code] tags.

Hey, next time you post, wrap your codes within the code-tag so that it will be easier to read.

Now, with your post.

Acending Order.

This is easy, you just have to do sort the array,

Arrays.sort(lengths);

then do a for loop to print the values.

here is the for loop.

for (int i = 0; i < lengths.length() - 1; i++) {
 System.out.println(lengths[i]);
}

Now for the descending order, you will still sort the array so that it will be lowest to highest. But since you need highest to lowest, you need to reverse the for loop so that it will start from the last element of lengths array.

Try coding this part (",)

Edited 3 Years Ago by mike_2000_17: Fixed formatting

This article has been dead for over six months. Start a new discussion instead.