• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

need help sorting a string array alphabetically  RSS feed

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can't seem to get this program to sort. Not sure why. Can anyone see a problem with my bubble sort?
thanks.
 
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Of course, the easy way is to just use java.util.Arrays.sort()
 
Ranch Hand
Posts: 348
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Jere Johnson
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 )
 
Jere Johnson
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks you to everybody who replied. I appreciate your time.
I tried the if (array[i]!=null) and it worked

 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!