Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Beginning Java and the fly likes need help sorting a string array alphabetically Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "need help sorting a string array alphabetically " Watch "need help sorting a string array alphabetically " New topic
Author

need help sorting a string array alphabetically

Jere Johnson
Greenhorn

Joined: Mar 29, 2003
Posts: 28
I can't seem to get this program to sort. Not sure why. Can anyone see a problem with my bubble sort?
thanks.


Diapers are the best invention
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1871
hi
this algo seems sorting in descending order. if u want to have ascending order sorting then change the comparision if() u 've like...
if ( a[i].compareTo(a[i-1]) < 0 )..
it shd work...
regards
maulin
Balaji Natarajan
Greenhorn

Joined: Dec 05, 2002
Posts: 28
Everything is Correct with ur code...the only thing is u haven't checked for null values while displaying and sorting...added those two checks...check whether it is working or not
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Of course, the easy way is to just use java.util.Arrays.sort()


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
chi Lin
Ranch Hand

Joined: Aug 24, 2001
Posts: 348
Jeremiah.
First, due to the uninitialized stringArray[], NullPointer exception was thrown when sort() method was called (check with MS-DOS for this exception). I put one line into init()
to initialize stringArray... current inital value is "".
for(int i=0;i<stringArray.length;i++)
stringArray[i]="";
Second, as I dont really see why the sort() does not work as expected, I made some changes on the index inside the for loop of the sort() in a way I know how sorting is done,

then re-run the applet, the result is not perfect, but sorting is on the way.
HTH
[ April 24, 2003: Message edited by: chi Lin ]

not so smart guy still curious to learn new stuff every now and then
Jere Johnson
Greenhorn

Joined: Mar 29, 2003
Posts: 28
Chi,
I tried it your way, but it seems like the sorting jumps about 8 index's down the array.
not sure why
your statement did get me somewhere. Thanks for that.
[ April 24, 2003: Message edited by: Jeremiah Coleman ]
Joel McNary
Bartender

Joined: Aug 20, 2001
Posts: 1817

Just as an aside, since I'm not sure what your assignment entails, I would suggest using a List instead of an Array to hold your Strings and then sorting your list.
Your sorting algorithm works fine, but as Balaji pointed out, the sortwing was failing when it encountered a null value in the Array. It woudl therefore do some sorting, but stop when it got the NullPointerException. If you filled the array with 10 variables it worked fine (albiet reversed; see Maulin's post).
in general, you would still have to check for null, but in this instance you cnnot put null values into the list (just empty strings). And as an added benefit, you List is not limited to 10 entries. You can add as many as you like (and then see the performance difference between bubble-sorting 10 items and 300 items )


Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
Jere Johnson
Greenhorn

Joined: Mar 29, 2003
Posts: 28
Thanks you to everybody who replied. I appreciate your time.
I tried the if (array[i]!=null) and it worked

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: need help sorting a string array alphabetically
 
Similar Threads
need help with string array using methods
need help with array program
need help with string array using methods
need help with string array
Fixing a bug on Adding & Deleting an Array index