• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Sorting Arrays

 
Zak Tacc
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm using the following code to sort a single dimensional array of strings, but any time it encounters a number, the sort resets itself. What is wrong with my code?



After sorting, the array becomes something like this:

apple
bun
cactus
elephant
zebra
13
android
babylon
zen
4
andy
band

and so on and so forth. Thanks



 
pete reisinger
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, you should use collection - they do the sorting for you (if they are sorted).
Check sorted set, sorted map etc.
collections
 
pete reisinger
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just checked your code and it outputs this:
13
4
android
andy
apple
babylon
band
bun
cactus
elephant
zebra
zen
The only thing I changed is method signature:
public static String[] selectionSort(String[] data) {

then of course I added return data; at the end.

If I output the array:
selectionSort(data);
for(String x : data) {
System.out.println(x);
}

it outputs the above result
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic