# Ascending Order

Page 1 of 1

## 3 Replies - 39940 Views - Last Post: 11 April 2007 - 02:07 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=25836&amp;s=11010def1159473845f3bb5110e178d1&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 wavyaquaeyes

Reputation: 0
• Posts: 7
• Joined: 12-December 05

# Ascending Order

Posted 01 April 2007 - 08:04 AM

HI Everyone,

I am new to java Programming. I am creating a program sp it prints 3 numbers in ascending order.

I know I need if statements and expressions(ie)

if (a > b && a > c)
print a
if (b > a && b >c)
print b
if (c > a && c >
print c
else if

[color=#FF0000]My problem is, I do not know what my next series of if expressions should be (I hope I am asking the question correctly_.

Thanks

Is This A Good Question/Topic? 0

## Replies To: Ascending Order

• g+ + -o drink whiskey.cpp

Reputation: 249
• Posts: 13,507
• Joined: 12-July 02

## Re: Ascending Order

Posted 01 April 2007 - 08:27 AM

There are a few ways to do what you're looking for. You mention that you are looking for ascending order - the code above, assuming it represents the first set of instructions, would print the largest first, meaning that your numbers would appear in descending order. You are looking for the smallest first.

The most basic of ways to order the numbers is brute force - check the combinations. Assuming three variables named a,b,c, then you could do something like the following:
```if(a<b&&a<c)
{
System.out.println(a);
if(b<c){
System.out.println(b);
System.out.println(c);
}
else{
System.out.println(c);
System.out.println(b);
}
}
else if(b<a&&b<c)
{
System.out.println(b);
if(a<c){
System.out.println(a);
System.out.println(c);
}
else{
System.out.println(c);
System.out.println(a);
}
}
else
{
System.out.println(c);
if(a<b){
System.out.println(a);
System.out.println(b);
}
else{
System.out.println(b);
System.out.println(a);
}
}

```

Of course, the example assumes that no two values are equal.

I would advise, however, that it may be a better idea to put the values into a java Array object, and use the included sort() method. No point in reinventing the wheel!

### #3 stew_downunder

Reputation: 0
• Posts: 33
• Joined: 04-April 07

## Re: Ascending Order

Posted 11 April 2007 - 01:58 AM

Amadeus, on 1 Apr, 2007 - 08:27 AM, said:

I would advise, however, that it may be a better idea to put the values into a java Array object, and use the included sort() method. No point in reinventing the wheel!

I am only a novice too here, but I have learned so far that it is always preferable to use the "far less code" way of doing things, especially when it comes to sorting which as Amadeus pointed out is much easier with an array sorting method.

I have included my example here for Wavyaquaeyes - I hope this is of help.

```import java.util.Arrays;

public class Wavyaquaeyes
{
public static void main(String args[])
{
char characterArray[] = {'w', 'a', 'v', 'y', 'a', 'q', 'u', 'a', 'e', 'y', 'e', 's'};

Arrays.sort(characterArray);

for (int i = 0; i < characterArray.length; i++)
{
System.out.print(characterArray[i]);
System.out.print(", ");
}
}
}
```

### #4 AngeluS

Reputation: 1
• Posts: 231
• Joined: 11-April 07

## Re: Ascending Order

Posted 11 April 2007 - 02:07 AM

If you are trying to learn Java then you should write a sort function yourself instead of using the sort method that already exists.
For sorting and ordering you will always need an array or list or any other way to have items "grouped" together.

What you should do is placing all your numbers in an array.
Then you loop write your own bubble sort and you let that method wreak havoc on your array. That way you can get easily and (quite) fast a sorted list of numbers.
If you want to know about simple sorting then check: http://en.wikipedia....iki/Bubble_sort