# Thread: Sorting in ascending and descending order

1. Member
Join Date
May 2011
Posts
8
Rep Power
0

## 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

Java 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] + " ");
}
}
}```

2. 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?
Last edited by Norm; 06-27-2011 at 05:06 AM.

3. Member
Join Date
May 2011
Posts
8
Rep Power
0
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] + " ");

4. Java 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.

5. 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.

6. 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}

7. 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

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

9. Member
Join Date
May 2011
Posts
8
Rep Power
0
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.

10. 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

11. 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.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•