wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Sorting Arrays 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 "Sorting Arrays" Watch "Sorting Arrays" New topic
Author

Sorting Arrays

Zak Tacc
Greenhorn

Joined: Feb 01, 2010
Posts: 25
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

Joined: Dec 30, 2009
Posts: 46
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

Joined: Dec 30, 2009
Posts: 46
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
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Sorting Arrays
 
Similar Threads
URGENT: Checking An Array For The Existance Of A Value
Sorting Asciibetically
Largest/Greatest of n numbers
String Input and Bubble Sort
I can NOT compile first class below