This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Need help with sorting an array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Need help with sorting an array" Watch "Need help with sorting an array" New topic
Author

Need help with sorting an array

Ben Buchli
Ranch Hand

Joined: Mar 26, 2004
Posts: 83
I'm completely lost.
I'm supposed to order an array of integers.
the original array is:

looks pretty good, but hey, there are numbers in there that shouldnt be there?! I have no idea how I just lose some numbers and the compiler adds some instead...

here is the code i have:

Any help is greatly appreciated!
Ben
[ April 19, 2004: Message edited by: Ben Buchli ]
Zhengwu Lee
Greenhorn

Joined: Oct 05, 2002
Posts: 3
The following will work correctly:

[ April 19, 2004: Message edited by: Zhengwu Lee ]
Java Programmer
Greenhorn

Joined: Apr 20, 2004
Posts: 9
PLease do the Following:
1. Insert your array to ArrayList as new ArrayList(array);
2. Use Collections.sort(arraylist)
3.Print Arraylist, You will get result
I'm supposed to order an array of integers.
the original array is:

looks pretty good, but hey, there are numbers in there that shouldnt be there?! I have no idea how I just lose some numbers and the compiler adds some instead...

here is the code i have:

Any help is greatly appreciated!
Ben
[ April 19, 2004: Message edited by: Ben Buchli ][/QB]


Smiling on Java
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Java Programmer,
Welcome to JavaRanch!
We ain't got many rules 'round these parts, but we do got one. Please change your display name to comply with The JavaRanch Naming Policy.
Thanks Pardner! Hope to see you 'round the Ranch!


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
the original array is:
---
looks pretty good, but hey, there are numbers in there that shouldnt be there?! I have no idea how I just lose some numbers and the compiler adds some instead...

When I run your algorithm, the result isn't as you've reported - no new numbers are introduced to the array, but the ordering of the components definitely isn't quite sorted.
As Zhengwu suggested, if you make that one small change to your algorithm, then I'm pretty sure you've got it figured out.
As the aptly named Java Programmer pointed out, when you're not just programming to learn, making use of existing libraries to perform routine tasks such as sorting can save quite a bit of effort for more important things. Note that if you wanted to go the "use the existing libraries" route to sort an array, converting it into an ArrayList is unnecessary. The utility class java.util.Arrays includes methods for sorting any primitive array, as well as for sorting an array of Comparable objects.
 
wood burning stoves
 
subject: Need help with sorting an array
 
Similar Threads
Convert Into Java Code.
Trees in Java
Project Euler problems with Scala
fields of visibility
Can't figure out what's wrong with this method