Sorting numbers stored in variables

I'm completely new at java and would like some help with a certain problem.I have 5 numbers, num1, num2, num3, num4, and num5.I have them stored in variables as listed. I'd like to know, is there a way to sort them using array.sort or would I have to use another method?
[293 byte] By [chrsharvick29a] at [2007-10-3 5:54:55]
# 1
You cannot invoke methods on arrays.
CaptainMorgan08a at 2007-7-15 0:35:38 > top of Java-index,Java Essentials,New To Java...
# 2
so is there a way to do this without writing out every combonation out in if/else statements?
chrsharvick29a at 2007-7-15 0:35:38 > top of Java-index,Java Essentials,New To Java...
# 3

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.

TuringPesta at 2007-7-15 0:35:38 > top of Java-index,Java Essentials,New To Java...
# 4

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

hunter9000a at 2007-7-15 0:35:38 > top of Java-index,Java Essentials,New To Java...
# 5
> sort(int[] a)You do mean Arrays.sort(int[] a) right?
CaptainMorgan08a at 2007-7-15 0:35:38 > top of Java-index,Java Essentials,New To Java...
# 6

@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!

prometheuzza at 2007-7-15 0:35:38 > top of Java-index,Java Essentials,New To Java...
# 7
I have them stored asint num1, num2, num3, ..etc.the values are stored in them already.if I didn't use array. what way could I sort them.
chrsharvick29a at 2007-7-15 0:35:38 > top of Java-index,Java Essentials,New To Java...
# 8
> if I didn't use array. what way could I sort them.With a lot if() statements!Better use an array.
prometheuzza at 2007-7-15 0:35:38 > top of Java-index,Java Essentials,New To Java...
# 9

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

hunter9000a at 2007-7-15 0:35:38 > top of Java-index,Java Essentials,New To Java...
# 10

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

chrsharvick29a at 2007-7-15 0:35:38 > top of Java-index,Java Essentials,New To Java...
# 11

> > 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);

TuringPesta at 2007-7-15 0:35:38 > top of Java-index,Java Essentials,New To Java...
# 12
You can. Put the values into an array, call Arrays.sort() on it, then print them out in a for loop. Or you could create your own method with a lot of if statements.
CaptainMorgan08a at 2007-7-15 0:35:38 > top of Java-index,Java Essentials,New To Java...
# 13

> ...

>

> 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));

prometheuzza at 2007-7-15 0:35:38 > top of Java-index,Java Essentials,New To Java...
# 14

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

TuringPesta at 2007-7-15 0:35:38 > top of Java-index,Java Essentials,New To Java...
# 15

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!

baftosa at 2007-7-21 11:20:07 > top of Java-index,Java Essentials,New To Java...
# 16

> 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

baftosa at 2007-7-21 11:20:07 > top of Java-index,Java Essentials,New To Java...
# 17
> I missed changed = false at the beginig of the loopThats why theres the handy-dandy edit feature, which you cant use anymore because you already replied to your own post.
CaptainMorgan08a at 2007-7-21 11:20:07 > top of Java-index,Java Essentials,New To Java...
# 18
prometheuzz, thank you very much. That works perfect for what I was trying to do.
chrsharvick29a at 2007-7-21 11:20:07 > top of Java-index,Java Essentials,New To Java...
# 19
> prometheuzz, thank you very much. That works perfect> for what I was trying to do.You're welcome. Note that other forum members also suggested (partly) the same solution as I just posted.
prometheuzza at 2007-7-21 11:20:07 > top of Java-index,Java Essentials,New To Java...
# 20
yes, thank you everyone who helped, I've been trying to figure a way to do this without using tons of if statements. thanks again
chrsharvick29a at 2007-7-21 11:20:07 > top of Java-index,Java Essentials,New To Java...
# 21

> 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

cafala at 2007-7-21 11:20:07 > top of Java-index,Java Essentials,New To Java...
# 22
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
ishanca at 2007-7-21 11:20:07 > top of Java-index,Java Essentials,New To Java...
# 23

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

Mr_Evila at 2007-7-21 11:20:07 > top of Java-index,Java Essentials,New To Java...
# 24
No need to reply. Ishanc has started his/her own thread here > http://forum.java.sun.com/thread.jspa?messageID=9447025
floundera at 2007-7-21 11:20:07 > top of Java-index,Java Essentials,New To Java...
# 25
Should I reply here?
cotton.ma at 2007-7-21 11:20:07 > top of Java-index,Java Essentials,New To Java...
# 26
No!As a penalty, all your dukes belong to us.
floundera at 2007-7-21 11:20:07 > top of Java-index,Java Essentials,New To Java...