No, you can indeed use a sort() method. It however belongs to the Arrays class which is a convenience class with static methods to deal with arrays.
http://java.sun.com/j2se/1.4.2/docs/api/java/util/Arrays.html
sort(int[] a)
Sorts the specified array of ints into ascending numerical order.
> I have 5 numbers, num1, num2, num3, num4, and num5.
>
> I have them stored in variables as listed.
Are you saying you have 5 different ints declared like
int num1, num2, etc.
or an int[] with length 5?
> I'd like
> to know, is there a way to sort them using array.sort
> or would I have to use another method?
If they are stored in an array you can use [url]http://java.sun.com/j2se/1.5.0/docs/api/java/util/Arrays.html#sort(int[])[/url]
to sort them.
@OP: Note that if you want to use the Arrays.sort(int[]) method suggested by TuringPest, you should put those variables in an integer array like this:
int[] array = {num1, num2, num3, num4, num5};
But after sorting, only the array will be sorted; the variables num1, num2, ... will still be the same!
> I have them stored as
>
> int num1, num2, num3, ..etc.
>
> the values are stored in them already.
>
> if I didn't use array. what way could I sort them.
I'm a little confused by what you're trying to do. Do you want the lowest value in num1, then next in num2, ... the highest value in num5?
If so then do like prometheuzz said in reply 6, and then sort the array. Then copy array[0] into num1, array[1] into num2, etc.
But unless for some reason you really need them to be stored in individual variables, it will probably be easier to only deal with the arrays.
Ok, to you guys your gonna say 'why didn't you say that in the first place'
The num1, num2, etc. have a value stored in them.
I want to print them out so that the values are print in ascending order.
lets say: num1 = 2, num2 = 1, num3 = 6, num4 = 3, num5 = 4.
I want it to print ; 1, 2, 3, 4, 6
I just thought I might be able to do it with array
> > if I didn't use array. what way could I sort
> them.
>
> With a lot if() statements!
> Better use an array.
Actually, if you want to be clever (but pointless, lol) you can do
int a, b, c;
when sorting:
obj[] objA = {a, b, c}
Arrays.sort(objA, Comparator);
> ...
>
> I want it to print ; 1, 2, 3, 4, 6
>
> I just thought I might be able to do it with array
Yes, it can. Here's a small example:
int num1 = 2, num2 = 1, num3 = 6, num4 = 3, num5 = 4;
int[] array = {num1, num2, num3, num4, num5};
java.util.Arrays.sort(array);
System.out.print(java.util.Arrays.toString(array));
> I just thought I might be able to do it with array
Id use arrays to begin with but if you want to be stubborn.
If you want to sort them you need them in a collection.
All collections need Objects not primitives so youre boned anyway.
My solution above is your best bet (without using arrays to begin with).
Arrays.sort has better performance than just mindless if/then/iterating.
Of course the arrays solution is the best, but if this is some sort
of useless exercise, a bubble sort can be implemented with
only 4 if statements. No arrays, no collections.
boolean changed;
int temp;
do {
if ( num1 > num2 ) { temp = num1; num1 = num2; num2 = temp; changed = true }
if ( num2 > num3 ) { temp = num2; num2 = num3; num3 = temp; changed = true }
if ( num3 > num4 ) { temp = num3; num3 = num4; num4 = temp; changed = true }
if ( num4 > num5 ) { temp = num4; num4 = num5; num5 = temp; changed = true }
} while ( changed );
I do not claim that it scales well for more than 5 values!
> Of course the arrays solution is the best, but if
> this is some sort
> of useless exercise, a bubble sort can be implemented
> with
> only 4 if statements. No arrays, no collections.
> [code]
> boolean changed;
> int temp;
> do {
> if ( num1 > num2 ) { temp = num1; num1 = num2; num2
> = temp; changed = true }
> if ( num2 > num3 ) { temp = num2; num2 = num3; num3
> = temp; changed = true }
> if ( num3 > num4 ) { temp = num3; num3 = num4; num4
> = temp; changed = true }
> if ( num4 > num5 ) { temp = num4; num4 = num5; num5
> = temp; changed = true }
> while ( changed );
> /code]
> I do not claim that it scales well for more than 5
> values!
I missed changed = false at the beginig of the loop
> You cannot invoke methods on arrays.
<pbs>
Arrays are class instances and can have methods invoked upon them.int[] vals = {1, 3, 5, 7, 9};
System.out.println(vals.toString());
System.out.println(vals.getClass().getName());
</pbs>
Message was edited by:
cafal
> Hey there, I'm curious, how would you sort 3 integers
> using only five if's and five elses and no other
> operators besides a < (less than) operator? Some help
> would be much appreciated! thanks. ishan
I can tell you that you don't do it by resurrecting old threads.