Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Need help with sorting an array

 
Ben Buchli
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The following will work correctly:

[ April 19, 2004: Message edited by: Zhengwu Lee ]
 
Java Programmer
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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]
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic