# Sorting in ascending and descending order

• 06-27-2011, 03:55 AM
flpanthers1
Sorting in ascending and descending order
So I have to write code for 5 numbers in ascending order, and then descending order. I have the code below, but my results are not what I need. I thought I could just switch to the > sign in the code. Apparently not. Any ideas would be helpful

Results:

Array of numbers in ascending and descending order
16 22 37 45 99

Code:

```public class IntArray2 {         public static void main(String[] args) {         int i =0;         int[] list = {45,37,22,16,99};         for(i = list.length-1; i > 0; i--){                 for(int k =0 ; k <list.length-1; k++ ){                 int tempa = list[k];                 int tempb = list[k+1];                         if (list[k] > list[k+1]){                         list[k] = tempb;                         list[k+1] = tempa;                         }                         }                         }         System.out.println("Array of numbers in ascending and descending order");                 for(int x =0; x < list.length; x ++)         {                 System.out.print(list[x] + " ");// Prints the numbers in ascending order         }                   for(int x =0; x > list.length; x ++) //Tried to switch to the > symbol thinking it would the print ascending order. Something is wrong here         {                 System.out.print(list[x] + " ");         }         } }```
• 06-27-2011, 04:01 AM
Norm
Quote:

results are not what I need.
If you would post what the results are and comment them saying why they are wrong it would speed things up.

Where is the code to sort the numbers in the other order?
• 06-27-2011, 04:30 AM
flpanthers1
The results from running the code is here...

Array of numbers in ascending and descending order
16 22 37 45 99

the code for the descending numbers is here... This is the last part of the code. I thought I could just change the < to a >. Apparently this is not right.

for(int x =0; x > list.length; x ++) //Tried to switch to the > symbol thinking it would the print ascending order. Something is wrong here
{
System.out.print(list[x] + " ");
• 06-27-2011, 04:32 AM
Junky
Code:

`for(int x =0; x > list.length; x ++)`
If the length of the list is 5 then how will it ever be possible for x (which is 0) to be greater? It will never enter this loop.
• 06-27-2011, 04:36 AM
Junky
What you are attempting to do is first sort the array and then print it in ascending and descending order. Is this correct? You might want to seek clarification as I believe you are supposed to sort in descending order as well.
• 06-27-2011, 06:38 AM
Dark
If you sort in ascending order and it works properly, write the opposite of the loop.

for (int i=list.length; i > -1; i--){//Code}
• 06-27-2011, 07:38 AM
DarrylBurke
Quote:

Originally Posted by Dark
If you sort in ascending order and it works properly, write the opposite of the loop.

for (int i=list.length; i > -1; i--){//Code}

Which blows up with a AIOOBE on the first iteration...

db
• 06-27-2011, 07:46 AM
Dark
My apologies,
for (int i=list.length-1; i > -1; i--){//Code}
• 06-27-2011, 02:56 PM
flpanthers1
Quote:

Originally Posted by Junky
What you are attempting to do is first sort the array and then print it in ascending and descending order. Is this correct? You might want to seek clarification as I believe you are supposed to sort in descending order as well.

Yes, this is correct. Ascending, and then into descending.
• 06-27-2011, 03:16 PM
JosAH
Quote:

Originally Posted by flpanthers1
Yes, this is correct. Ascending, and then into descending.

You can leave your sorting algorithm as it is but look upon each number x in the array to be sorted as if it were -x, x < y <---> -x > -y

kind regards,

Jos
• 06-27-2011, 03:48 PM
Dark
If you look at my second post, I put the loop you would use for your current sorting algorithm. You just reverse the loop you use to print it in ascending order.